Flow of control, variables, and constants in JavaScript

We start with aspects in which JavaScript is very similar to other programming languages. We will cover the characteristics and syntaxThe rules for expression. quickly so that we can progress to more substantial topics. If at any point the coverage seems too brief, feel free to review the background material found earlier in this module.

Flow of control

A program is a listing of instructions for the computer, and the flow of control is the order in which the instructions are followed. Eventually, we will discuss complex ways to specify that some instructions are to be skipped or repeated more than once. For now, we will consider only simple flow of control, in which instructions are each executed once, in the order in which they appear in the program.

In JavaScript, each separate statement is ended with a semicolon. There are times when semicolons are not needed; for simplicity we will use semicolons at those times as well. You can ignore warnings about extra semicolons that may appear in the course examples and exercises. Don't be surprised if sometimes you accidentally omit a semicolon but the program still runs correctly.

Variables

A variable is declaredTo give the name of a variable or constant and possibly the type of data. using a var statement, as seen in the example below, where a variableThe name used to refer to some data and the address (location) where it is stored; this is allowed to change. cost is being declared.

var cost;

It is possible to declareTo give the name of a variable or constant and possibly the type of data. and initializeTo give the initial value of a variable or constant. a variable in the same line, by adding = and the value.

var cost = 100;

Even more space can be saved by using a comma to join several declarations or declarations and initializations on the same line. That said, the use of space should not be your main concern: make choices that provide better clarity, which in some cases might entail putting related items together on one line and in others might entail using extra lines so that commentsInformation written for humans, ignored by the computer. are easy to insert.

var cost = 100, taxRate = .15;

The value of a variable that has not been initialized is the JavaScript value undefined. If you accidentally redeclare a variable that has already been declared and initialized, the value doesn't change (unless you assign a value at the same time as the second declaration). JavaScript does not warn you or stop you if you declare a variable a second time, as in the example below.

var cost = 100;
var cost;

JavaScript is loosely typed, which means that a variable storing a value of one data typeA type of data, such as a number or a string. can store a value of another data type. To make matters even more confusing, JavaScript will "help" you in certain circumstances by changing the type of a value to what you seem to want it to be. Later in this module, we will discuss both automatic type conversion and how to avoid unpleasant surprises it might cause.

Declaring and initializing constants

Although a const statement has been introduced in JavaScript, how and whether it is supported on older browsers (and in older versions of JavaScript) may make it somewhat risky to use. It has most of the same properties as mentioned above for var, except (when implemented properly) it is not possible to declare a constant more than once or to assign it a value more than once.

A safer approach may be to use var instead, and use all capital letters in the name of the constant as a reminder that its value should not change.

Identifiers

Make sure to follow all these rules when creating an identifierThe name that you choose for a variable or another creation.:

  • The first character is a letter, $, or _.
  • Any other character is a letter, $, _, or a digit.
  • There are no spaces.
  • The identifier cannot be a keyword, such as var.

The conventionA consistent style used to make code more readable to humans. for variables is to use camelCase, in which the first word is in lower case and each subsequent word has its first letter capitalized. For constants, the convention is to use all capitals.

In our examples, cost and taxRate are both identifiers, and are both written in camelCase.