Take an example as below.
5 % 2 => 1
This is an example of modulo operation. 5 is called dividend, 2 is called divisor and the result 1 is called remainder.
In JavaScript, we can do modulo operation as below.
> 5 % 3
2
> -5 % 3
-2
> 0 % 3
0
> 3 % 0
NaN
Sometimes, we may use modulo operations in various complicated arithmetic operations. So it is important to know some properties of modulo operations. Let's explore some mostly used ones.
(a % n) % n === a % n
> (5 % 2) % 2 === 5 % 2
true
(n ^ x) % n === 0
> Math.pow(5,3) % 5
0
((-a % n) + (a % n)) % n === 0
> ((-13 % 3) + (13 % 3)) % 3 === 0
true
(a + b) % n === ((a % n) + (b % n)) % n
> (3 + 5) % 3 === ((3 % 3) + (5 % 3)) % 3
true
(a * b) % n === ((a % n) * (b % n)) % n
> (3 * 7) % 2 === ((3 % 2) * (7 % 2)) % 2
true