JavaScript Remainder Operator
JavaScript Remainder Operator
Donate Now
(https://www.javascripttutorial.net/donation/)
dividend % divisor
https://www.javascripttutorial.net/javascript-remainder-operator/ 1/5
29/07/2022, 07:58 JavaScript Remainder Operator
In this equation, the dividend , divisor , quotient , and remainder are all integers. The sign of
the remainder is the same as the sign of the dividend .
The sign of the remainder is the same as the sign of the dividend .
The following example shows how to use the remainder operator with a positive dividend:
console.log(remainder); // 1
remainder = 5 % 2;
console.log(remainder); // 1
The following example uses the remainder operator with a negative dividend:
let remainder = -5 % 3;
console.log(remainder); // -2
remainder = -5 % -3;
console.log(remainder); // -2
If a dividend is an Infinity and a divisor is a finite number, the remainder is NaN . For example:
https://www.javascripttutorial.net/javascript-remainder-operator/ 2/5
29/07/2022, 07:58 JavaScript Remainder Operator
console.log(remainder); // NaN
let remainder = 10 % 0;
console.log(remainder); // NaN
console.log(remainder); // NaN
If a dividend is a finite number and the divisor is an Infinity , the remainder is the dividend. For
example:
console.log(remainder); // 10
If the dividend is zero and the divisor is non-zero, the remainder is zero:
console.log(remainder); // 0
If either dividend or divisor is not a number, it’s converted to a number using the Number() function
and applied the above rules. For example:
console.log(remainder); // 1
https://www.javascripttutorial.net/javascript-remainder-operator/ 3/5
29/07/2022, 07:58 JavaScript Remainder Operator
To check if a number is an odd number, you use the remainder operator ( % ) like the following
example:
In this example, if the num is an odd number, the remainder is one. But if the num is an even
number, the remainder is zero.
function isOdd(num) {
return num % 2;
If you have been working with Python, you may find the % represents the modulo operator in this
language. However, it is not the case in JavaScript.
https://www.javascripttutorial.net/javascript-remainder-operator/ 4/5
29/07/2022, 07:58 JavaScript Remainder Operator
Or wrap it in a function:
If the division and divisor have the same sign, the remainder and modulo operators return the same
result. Otherwise, they return different results.
For example:
console.log('remainder:', 5 % 3); // 2
console.log('remainder:', -5 % 3); // -2
Output:
remainder: 2
modulo: 2
remainder: -2
modulo: 1
Summary
Use the JavaScript remainder operator ( % ) get the the remainder of a value divided by another
value.
https://www.javascripttutorial.net/javascript-remainder-operator/ 5/5