What is the modulo operator in JavaScript?

The modulo operator is represented by the % character in JavaScript. It returns the remainder of two integers that have been divided.

svg viewer
remainder = 7 % 5
console.log(remainder)

As the remainder will always be less than the divisor, modulo operators can be very useful in restricting the range of outputs. This makes modulo operators particularly popular in Hash functions.

for(var i=0; i<10; i+=1)
{
console.log( i % 4 ) //As divisor is 4, remainder will range from 0 to 3
}
Copyright ©2024 Educative, Inc. All rights reserved