Simple Statements
Some JavaScript statements extend beyond a line, such as those
for
loops, which have a beginning and
end. Others, though, stand all on their own: one statement, one line.
Among these simple statements are those for assignment.
The Assignment Statement
The most common statement is the assignment statement. Itâs an expression consisting of a variable on the left side, an assignment operator (=), and whatever is being assigned on the right.
The expression on the right can be a literal value:
nValue = 35.00;
Or, a combination of variables and literals combined with any number of operators:
nValue = nValue + 35.00;
And it can be a function call:
nValue = someFunction(ââ);
More than one assignment can be included on a line. For instance, the following assigns the value of an empty string to multiple variables, on one line:
var firstName = lastName = middleName = "";
Following the assignment statement, the second most common type of statement is the arithmetic expression that involves the arithmetic operators, discussed next.
Arithmetic Statements
In the last section, the second example was a demonstration of a binary arithmetic expression: two operands are separated by an arithmetic operator, leading to a new result. When paired with an assignment, the result is then assigned to the variable on the left:
nValue = vValue + 35.00;
More complex examples can use any number of arithmetic operators, with any combination of literal values and variables:
nValue = nValue + 30.00 / 2 ...
Get Learning JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.