Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
What is Modulus Operator (%) in JavaScript?
The modulus operator (%) in JavaScript returns the remainder after dividing one number by another. It's also called the remainder operator and is commonly used in programming for tasks like checking if a number is even or odd.
Syntax
operand1 % operand2
Where operand1 is the dividend and operand2 is the divisor. The operation returns the remainder of the division.
Basic Examples
console.log(10 % 3); // 1 (10 ÷ 3 = 3 remainder 1) console.log(15 % 4); // 3 (15 ÷ 4 = 3 remainder 3) console.log(20 % 5); // 0 (20 ÷ 5 = 4 remainder 0) console.log(7 % 2); // 1 (odd number) console.log(8 % 2); // 0 (even number)
1 3 0 1 0
Handling Negative Numbers
The sign of the result depends on the dividend (first operand):
console.log(13 % 5); // 3 (positive dividend) console.log(-13 % 5); // -3 (negative dividend) console.log(13 % -5); // 3 (positive dividend) console.log(-13 % -5); // -3 (negative dividend)
3 -3 3 -3
Special Cases
The modulus operator returns NaN in certain situations:
console.log(10 % 0); // NaN (division by zero) console.log(Infinity % 5); // NaN (dividend is Infinity) console.log(NaN % 5); // NaN (operand is NaN) console.log(5 % NaN); // NaN (operand is NaN)
NaN NaN NaN NaN
Browser Example with Interactive Input
<html>
<body>
<h2>Modulus Operator Calculator</h2>
<label>First Number: </label>
<input type="number" id="num1" value="17"> <br><br>
<label>Second Number: </label>
<input type="number" id="num2" value="5">
<p>Click to calculate the remainder:</p>
<p id="result"></p>
<button onclick="calculate()">Calculate</button>
<script>
function calculate() {
var num1 = Number(document.getElementById("num1").value);
var num2 = Number(document.getElementById("num2").value);
var remainder = num1 % num2;
document.getElementById("result").innerHTML =
"<b>" + num1 + " % " + num2 + " = " + remainder + "</b>";
}
</script>
</body>
</html>
Common Use Cases
// Check if number is even or odd
function isEven(num) {
return num % 2 === 0;
}
// Cycle through array indices
function getNext(currentIndex, arrayLength) {
return (currentIndex + 1) % arrayLength;
}
// Check if year is leap year (simplified)
function isLeapYear(year) {
return year % 4 === 0;
}
console.log("5 is even:", isEven(5)); // false
console.log("6 is even:", isEven(6)); // true
console.log("Next index after 4 in array of 5:", getNext(4, 5)); // 0
console.log("2024 is leap year:", isLeapYear(2024)); // true
5 is even: false 6 is even: true Next index after 4 in array of 5: 0 2024 is leap year: true
Conclusion
The modulus operator (%) is essential for finding remainders, checking even/odd numbers, and creating cyclic operations. Remember that the result's sign matches the dividend, and division by zero returns NaN.
Advertisements
