fancy fancy - 7 months ago 64
Javascript Question

What does % do in JavaScript?

What does the % do in JavaScript?

A definition of what it is and what it does would be much appreciated.


It's a modulo operator. See this documentation or the specification for more information on JavaScript arithmetic operators.

% (Modulus)

The modulus operator is used as follows:

var1 % var2

The modulus operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the preceding statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2. For example, 12 % 5 returns 2. The result will have the same sign as var1; that is, −1 % 2 returns −1.