Dividing numbers, in most cases, is an intricate and complicated matter. Say, the first operand may be less than the second, they both could be zeros, or exactly the same, or may be divisible without remainder, or may give the remainder of the divisor, etc.

When two numbers are divisible by the remainder, we have to define what operator will be responsible for it. Well, where there is a demand, there is a supply. In JavaScript we have a thing called modulo operator.

Syntax and operands

This operator can be called a module operator, a remainder, or a modulus operator. In any case, this operator is the remainder that is returned when one operand is divided by another operand. The syntax of the module looks as follows:

x % y

By the way, in some languages operands can only be integers. However, in JavaScript these values can be both integers and float numbers.

Return value

Modulo operators return a value remainder obtained by dividing two operands x and y. If two operands are positive numbers - no questions arise, it is clear that the value that the operator returns will also be positive.

But it is possible to assume that one of the numbers may be a negative value. In such a case the module operator behaves like a remainder operation and gives a negative result in case the first operand is negative. But this does not apply if the first operand is positive and the second is negative.

If both operands are negative, the result will be positive.

Note that for any x mod 1 is equal to x. Regarding zero, we know that it cannot be divided. Accordingly, the result for the expression x mod 0 will be indefinite.

In case if the first or second operand is NaN, the result is also NaN. If the first operand is a finite value and the second operand is infinity, then the result is equal to the first value. If the first value is zero and the second is not zero and finite, the result will be the same as the second value.

In other cases, when none of the values is equal to infinity, zero and NaN, the result from x mod y is determined by the mathematical relation:

const result = x - (y * i);

Where i: x/y = i (remainder result) or x = y * i + result.

It should be noted that the remainder as a result will always be less than the second operand.

Examples

Let's look at some simple examples to elaborate on the topic.

14 % 3  //  2 as a result
2 % -4  //  2 as a result
2 % 4   //  2 as a result
8 % 10  //  8 as a result
 
7.25 % 2    // 1.25 as a result
 
-14 % 5 // -4 as a result
-1 % 2  // -1 as a result
-6 % 3  // -0 as a result
 
NaN % 25 // NaN as a result
 
Infinity % 9        // NaN as a result
Infinity % 0        // NaN as a result
Infinity % Infinity // NaN as a result

One of the simplest applications of the modulo is to check numbers for evenness and oddness. By the way, this is how this operator is used most often. All you have to do is divide the number by two. If the result is zero, then the number is even, otherwise the number is odd.

num % 2 == 0    // will return the true if the number is even or return false
num % 2 == 1
num % 2 != 0    // these two entries mean the same thing and both will return true if the number is odd

The operator module can also be useful in case you want to get a fractional part of a decimal number. all you need to do is specify the number 1 in the second operand.

12.04 % 1;  // return 0.04 as a result

By the way, everything will work correctly if two operands are decimal numbers. Let's consider this brief example.

3 % 0.9 // return 0.3 as a result

And now, a slightly more complex example. Say, we have a group of students. Each student has his own number. The number of students in this group is 32. For a test task the teacher has prepared 3 variants. We have to determine which student should perform which variant of a test.

const numberOfStudents = 32;
 
for (let i = 1; i <= numberOfStudents; i++) {
 if (i % 3 === 0) {
   console.log("Your variant of test is - '3' ");
 } else if (i % 2 === 0) {
   console.log("Your variant of test is - '2' ");
 } else {
   console.log("Your variant of test is - '1' ");
 }
}

// Output:
// Your variant of test is - '1'
// Your variant of test is - '2'
// Your variant of test is - '3'
// Your variant of test is - '2'
// Your variant of test is - '1'
// Your variant of test is - '3'
// ...

We first declare the number of students in the group. Then, with the help of the for cycle, each student receives a message about which variant he should do. We do this using if conditions, and check whether the number is divisible by 2, then by 3, and finally by 1.

As seen, we first went through options where the student number is divisible by 2 and 3 without remainder. How so? Because if first we were to specify the condition with the number divisible by 1, all students would have written just one test variant.