Trusted answers to developer questions

What is the modulo operator in JavaScript?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

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
}

RELATED TAGS

javascript
modulo
arithmetic
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?