JavaScript

How to use the modulo operator (%) in JavaScript?

JavaScript is a web-based programming language that makes our web pages and web applications dynamic by giving them the ability to think and act. Like any other programming language JavaScript also offers operands and operators. Operators are symbols that instruct our system to perform an arithmetic task like addition, subtraction, and multiplication. Operands are the values that the operator performs actions on. One example of an operator is the modulo operator.

What is a modulo operator?

A modulo operator also referred to as remainder or modulus operator is the remainder that is returned when some operand is divided with another operand. The Syntax for the modulo operator is:

A % B

We will read as A mod B where A and B are operands and the remainder that will be returned from the above operation will take the sign of the dividend.

Example:

Let us go through the below example where we will console log the result of the modulo operation:

console.log(15 % 5); //0

console.log(17 % 5); //2

console.log(-17 % 5); //-2

Let us now explore other areas where we can apply the modulo operators so we can get a grip on how to use the modulo operator in JavaScript.

Testing number is divisible by another number

Let us check whether a number is divisible by another number or not with the help of the modulo operator:

var num=4;
console.log(num % 4 == 0 );// true as num is divisible by 4
console.log(num % 3 == 0 );// false as num is not divisible by 3

We initiated a variable with the name of num and gave it the value of 4. After which, we console log some operations to test whether num is divisible by 4 or not.

Checking Even and Odd number

Another advantage we can take from the modulo operator is to check whether a number is odd or not using the modulo operator:

var num=4;
console.log(num % 2 == 0 );// true as num is even number
console.log(num % 2 != 0); // false as num is odd number

A variable num is initiated first and then with the help of the modulo operator we check when the num is divided with 2, the result is 0(num is even) or not(num is odd).

Getting the Fractional part of a number

Suppose you encounter the problem of finding the fractional part of a number i-e the number after the decimal point for example if you have a number 2.25 then extract the 0.25 from that number and return it. To achieve this task, we will take help from the modulo operator:

functiongetFractional(num) {
return num % 1;
}

console.log(getFractional(2.25)); // 0.25

Conclusion

Like any other programming language, JavaScript offers us operators and operands. For example, In num1 + num2, num1 and num2 are operands, and math + is the operator. JavaScript offers us the modulo operator as well which returns the remainder of two operands.

In this post, we saw what the modulo operator is and how to use the modulo operator in JavaScript with the help of solving problems like finding even and odd numbers and extracting the fractional part of a number.

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.