JavaScript

What does a modulus operator do in JavaScript?

JavaScript is one of the most widely used dynamic and scripting programming languages for creating web pages and applications and like any other programming language, JavaScript offers operators which are specific symbols to perform operations on the operand. For example:

3-2; //1

In the above example, 2 and 3 are operands, and “-” is an operator.

JavaScript offers a bunch of operator types in which one of them is the Arithmetic Operator that is used to perform an arithmetic operation, for example:

var add = 3+2; //5

In the above example, the operator + is used to add the two numbers 3 and 2. The arithmetic operators include +, -, *, /, %, ++, –, and **.

What is Modulus Operator?

The modulus operator is also called modulo operator and remainder operator and it returns the remainder of a division sum when the first operand is divided by the second operand, the remainder is returned.

Syntax:

A % B

It is read as A mod B and A and B are operands.

Example:

var num1=9;
var num2=2;
var num3=3;
alert(num1%num2 +", "+ num1%num3 ); //1,0

In the above example, we initiated three numbers and then checked the mod with num1 and num2 and num1 and num3. The output of the above code is:

Finding Even and Odd number

The modulus operator is very useful in finding whether a number is even or odd. For example, an integer is even if it is divisible by 2 and remainder is zero and an integer is odd when the remainder is not zero. So, we can use the modulus operator and find whether an integer is even by finding the remainder. If integer%2 === 0 then the number is even and if the integer%2 ===1 then the integer is odd. Let’s implement the above theory in JavaScript:

function checkEven(num) {
  return num % 2 === 0;
}
function checkOdd(num) {
  return num % 2 === 1;
}

console.log(checkEven(4)); // true
console.log(checkEven(8.6)); // false
console.log(checkEven(7)); // false

console.log(checkOdd(5)); // true
console.log(checkOdd(8.6)); // false
console.log(checkOdd(8)); // false

Finding Fractional part of a number

Let’s look at another problem where we are asked to find the fractional part of the number i-e the number after the decimal point for example if you have a number 3.5, we have to extract the 0.5 and return it. We can use the modulus operator to get our desired result in the following manner:

function getFractional(num) {
  return num % 1;
}

console.log(getFractional(3.5)); // 0.5

Conclusion

After dividing two numbers, the function of the modulus or modulo operator is to calculate the remainder. In this article, we saw what modulus operator are and what modulus operator does in JavaScript along with examples. We’re confident that after reading this tutorial, you’ll be ready to use the Javascript modulus operator like a pro.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.