HTML and CSS Reference

In-Depth Information

EXPLANATION

1

JavaScript program starts here.

2

The variable
num
is assigned
10
.

3

Output is sent to the browser.

4

The shortcut assignment operator, +=, adds 2 to the variable
num.
This is equiva-

lent to
num = num + 1;
.

5

The shortcut assignment operator, -=, subtracts 1 from the variable
num.
This is

equivalent to
num = num - 1;
.

6

The shortcut assignment operator, *, multiplies the variable
num
by 3. This is

equivalent to
num = num * 3;
.

7

The shortcut assignment modulus operator, %, yields the integer amount that re-

mains after the scalar
num
is divided by 5. The operator is called the modulus op-

erator or remainder operator. The expression
var %= 5
is equivalent to
num
=
num

% 5
;.

8

JavaScript ends here. The output is shown in Figure 5.4.

Figure 5.4
Output from Example 5.4: Shortcut operators.

To make programs easier to read, to simplify typing, and, at the machine level, to

produce more efficient code, the autoincrement (++) and autodecrement (- -) oper-

ators are provided.

The autoincrement operator performs the simple task of incrementing the value of its

operand by 1, and the autodecrement operator decrements the value of its operand by

1. The operator has two forms: the first form
prefixes
the variable with either ++ or - -

(e.g.,
++x
or - -
x)
; the second form
postfixes
(places the operator after) the variable

name with either ++ or - - (e.g.,
x++
or
x
- -). For simple operations, such as x++ or

x- -, ++x or - -x, the effect is the same; both
++x
and
x++
add one to the value of x, and

both - -
x
and
x
- - subtract one from the value of
x
.