O'Reilly logo

Learning JavaScript by Shelley Powers

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 - nValue2 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required