↑
Main Page
Divide
Divide
The divide operator is represented by a slash (
/
) and divides the first operand by the second operand:
var iResult = 66 / 11;
The divide operator, like the multiply operator, has special behaviors for special values:
?
If the operands are numbers, regular arithmetic division is performed, meaning that two posi-
tives or two negatives equal a positive, whereas operands with different signs yield a negative.
If the result is too high or too low, the result is either
Infinity
or –
Infinity
.
?
If either operand is
NaN
, the result is
NaN
.
?
If
Infinity
is divided by
Infinity
, the result is
NaN
.
?
If
Infinity
is divided by any number, the result is
Infinity
.
?
Division of a non-infinite number by 0 always equals
NaN
.
?
If
Infinity
is divided by any number other than 0, the result is either
Infinity
or –
Infinity
, depending on the sign of the second operand.
Modulus
The modulus (remainder) operator is represented by a percent sign (
%
) and is used in the following way:
var iResult = 26 % 5; //equal to 1
Just like the other multiplicative operators, the modulus operator behaves differently for special values:
?
If the operands are numbers, regular arithmetic division is performed, and the remainder of that
division is returned.
?
If the dividend is
Infinity
or the divisor is 0, the result is
NaN
.
?
If
Infinity
is divided by
Infinity
, the result is
NaN
.
?
If the divisor is an infinite number, the result is the dividend.
?
If the dividend is 0, the result is 0.
Additive operators
The additive operators, add and subtract, are typically the simplest mathematical operators in program-
ming languages. In ECMAScript, however, a number of special behaviors are associated with each
operator.
Add
The add operator (
+
) is used just as one would expect:
var iResult = 1 + 2;
47
ECMAScript Basics
05_579088 ch02.qxd 3/28/05 11:35 AM Page 47
Free JavaScript Editor
Ajax Editor
©
→ R7