Name
- - — NN 2 IE J1 ECMA 1
Synopsis
The decrement operator. This unary operator subtracts 1 from the current value of a variable expression. You can place the operator in front of or behind the variable for a different effect. When the operator is in front of the variable, the variable is decremented before it is evaluated in the current statement. For example, in the following sequence:
var a, b a = 5 b = --a
one is subtracted from a before being assigned to
b. Therefore, both b and
a are 4 when these statements finish running. In
contrast, in the following sequence:
var a, b a = 5 b = a--
the subtraction occurs after a is assigned to
b. When these statements complete,
b is 5 and a is 4.
This behavior impacts the way for-loop-counting
variables are defined and used. Typically, a loop counter that counts
backwards from a maximum value decrements the counter after the
statements in the loop have run. Thus most loop counters place the
operator after the counter variable:
for (var i = 10; i >=0; i--) ...
Example
--n n--
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access