HOWTO · JavaScript
Modulo Operator(%) in JavaScript
Learn how JavaScript's % operator returns a remainder, how negative operands and special numeric values behave, and how to use it for parity, fractional parts, intervals, turns, and time conversion.
On this page
JavaScript’s % operator returns the remainder after division. For positive values this often matches what people call modulo, but negative dividends make the distinction visible: JavaScript’s remainder has the sign of the dividend. This guide shows the syntax, special numeric cases, and common patterns.
What the JavaScript % Operator Returns
For finite numbers, a % b returns the remainder after truncating the quotient a / b toward zero. In 10 % 3, the quotient is 3 and the remainder is 1. The result follows the dividend (a) sign, so -10 % 3 is -1. This sign rule is why JavaScript % is a remainder operator and does not always produce mathematical modulo’s nonnegative result.
Syntax and Basic Examples
Write the dividend, %, then the divisor: dividend % divisor. The operator accepts numeric operands, including fractions. In the expression, the value on the left is divided by the value on the right, and the result is a number that your code can store, compare, or pass to another calculation. The example assigns each result to a named constant and sends both values to the console:
const wholeRemainder = 10 % 3;
const fractionalRemainder = 5.5 % 2;
console.log(wholeRemainder);
console.log(fractionalRemainder);
The first result is 1; the second is 1.5, because 2 fits into 5.5 twice with 1.5 left over. The supplied runtime run confirms these two outputs: 1 and 1.5.
Negative Numbers and Edge Cases
The remainder has the dividend’s sign: 10 % 3 is 1, -10 % 3 is -1, and 10 % -3 is 1. A negative exact multiple can produce negative zero, as in -4 % 2; compare it with Object.is(-4 % 2, -0) when the sign of zero matters.
JavaScript returns NaN if either operand is NaN, if the dividend is infinite, or if the divisor is 0 or -0. A finite dividend with an infinite divisor returns the dividend unchanged, so 5 % Infinity is 5. These outcomes follow JavaScript’s numeric remainder semantics; they are not results observed in a run supplied with this packet.
Common Uses of the % Operator
For integer inputs, parity checks use n % 2 === 0 for even and n % 2 !== 0 for odd. This works with negative integers too, because their remainders are 0 or -1. If the input might be a fraction or a numeric string, validate or normalize it before using an integer parity check.
For a finite number, value % 1 gives the signed fractional remainder; for example, 5.5 % 1 is 0.5, while -5.5 % 1 is -0.5. To get a positive fractional part for negative values, choose an explicit normalization rule rather than assuming % 1 is nonnegative.
For a nonnegative whole number of minutes, hours = Math.floor(minutes / 60) and remainingMinutes = minutes % 60 split the value into hours and minutes. For example, 135 minutes becomes 2 hours and 15 minutes.
To run an action every third pass through a loop, test i % 3 === 0. To alternate between two entries in an array, use (round - 1) % players.length when round starts at 1; subtracting one makes the first turn use index 0. These patterns assume a positive interval or a nonempty array, respectively.
Remainder and Mathematical Modulo
Mathematical modulo commonly returns a value from 0 up to, but not including, a positive modulus. JavaScript % instead keeps the dividend’s sign, so -10 % 3 is -1. For a finite positive modulus n, the expression ((a % n) + n) % n normalizes a finite a to a nonnegative modulo-style result. For example, it maps -10 with modulus 3 to 2. Keep the positive-modulus assumption explicit; this pattern is not a general replacement for % with arbitrary divisors.