Remainder (%) (Operator/multiplicative)

Divides one operand by another and yields the remainder.


ECMAScript edition - 2
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Netscape Enterprise Server - 2.0
Opera - 3.0
Property/method value type:Number primitive
JavaScript syntax:-anOperand1 % anOperand2
Argument list:anOperand1The dividend value
anOperand2The divisor value

The % operator yields the remainder after dividing the left operand by the right. This is otherwise known as the modulo or modulus operator.

In an ECMAScript compliant interpreter, the remainder is a floating-point value and, unlike the C and C++ languages, the input operands can also be floating point values.

The result of performing a remainder on floating point values is not the same as the IEEE 754 remainder operation. IEEE 754 mandates a remainder computed via a rounding division whereas ECMA mandates a truncating remainder. In ECMAScript the remainder behaves similarly to the Java integer remainder operator and is analogous to the fmod() library function in C language compilers.

Otherwise, as long as neither an infinity, zero or NaN is involved, the floating remainder is calculated like this. The dividend n is divided by the divisor d to produce a quotient, q. The quotient is forced to be an integer and multiplied again by d and the result subtracted from n to yield the remainder. Thus:

q = n/d

r = n - (d * q)

The value q will be forced to be an integer although the resulting value r may not be.

The associativity is left to right.

Refer to the operator precedence topic for details of execution order.


See also:Arithmetic operator, Associativity, Divide (/), Divide then assign (/=), Integer arithmetic, Integer-value-remainder, Math.ceil(), Math.floor(), Multiplicative expression, Multiplicative operator, Operator Precedence, Remainder then assign (%=)


ECMA 262 edition 2 - section - 11.5.3

ECMA 262 edition 2 - section - 11.13

ECMA 262 edition 3 - section - 11.5.3

Wrox Instant JavaScript - page - 19