Main Page

Comma operator

var iNum = 10;
iNum += 10;
Compound assignment operators exist for each of the major mathematical operations and a few others
as well:
?
Multiply/Assign (
*=
)
?
Divide/Assign (
/=
)
?
Modulus/Assign (
%=
)
?
Add/Assign (
+=
)
?
Subtract/Assign (
-=
)
?
Left Shift/Assign (
<<=
)
?
Signed Right Shift/Assign (
>>=
)
?
Unsigned Right Shift/Assign (
>>>=
)
Comma operator
The comma operator allows execution of more than one operation in a single statement. Example:
var iNum1=1, iNum2=2, iNum3=3;
Most often, the comma operator is used in the declaration of variables.
Statements
ECMA-262 describes several
statements
for ECMAScript. Essentially, statements define most of the syn-
tax of ECMAScript and, typically, use one or more keywords to accomplish a given task. Statements can
be simple, such as telling a function to exit, or complicated, such as specifying a number of commands to
be executed repeatedly. This section introduces all the standard ECMAScript statements.
The if statement
One of the most frequently used statements in ECMAScript (and indeed, in many languages), is the
if
statement. The
if
statement has the following syntax:
if (
condition
)
statement1
else
statement2
The condition can be any expression; it doesn’t even have to evaluate to an actual Boolean value.
ECMAScript converts it to a Boolean for you. If the condition evaluates to
true
, statement1 is executed;
if the condition evaluates to
false
, statement2 is executed. Each of the statements can be either a single
line or a code block (a group of code lines enclosed within braces). For example:
53
ECMAScript Basics
05_579088 ch02.qxd 3/28/05 11:35 AM Page 53


JavaScript EditorFree JavaScript Editor     Ajax Editor


©

R7