Having explained operator precedence, associativity, and other background material, we can start to discuss the operators themselves. This section details the arithmetic operators:
While the modulo operator is typically used with integer operands, it also works for floating-point values. For example, -4.3 % 2.1 evaluates to -0.1.
var profit = +1000000;
In code like this, the + operator does nothing; it simply evaluates to the value of its argument. Note, however, that for non-numeric arguments, the + operator has the effect of converting the argument to a number. It returns NaN if the argument cannot be converted.
For example, the following code sets both i and j to 2:
i = 1; j = ++i;
But these lines set i to 2 and j to 1:
i = 1; j = i++;
This operator, in both of its forms, is most commonly used to increment a counter that controls a loop. Note that, because of JavaScript's automatic semicolon insertion, you may not insert a line break between the post-increment or post-decrement operator and the operand that precedes it. If you do so, JavaScript will treat the operand as a complete statement by itself and will insert a semicolon before it.
Copyright © 2003 O'Reilly & Associates. All rights reserved.