can someone explain me this javascript syntax?
i know it returns if number is or isnt divisible by other by returning 0 or 1, but i dont understand the syntax. What is 12%2 for example?
use case example: 12%2 === 0 //true
can someone explain me this javascript syntax?
i know it returns if number is or isnt divisible by other by returning 0 or 1, but i dont understand the syntax. What is 12%2 for example?
use case example: 12%2 === 0 //true
% is modulo, or called mod, the remainder when dividing. For example, “5 % 3 = 2” which means 2 is the remainder when you divide 5 by 3.
Back to your question, 12 % 2 equals 0 is because you do not have any reminder left from the dividing.
In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another
This essentials means:
What is the amount left over when dividing X by Y
Let's say you have 30 eggs and you only have 2 cartons with 12 spots for those eggs. Modulo is the amount of eggs left over that can't make a complete carton (6 in this case).
30 % 12 => 6
Some more examples:
5 % 2 => 1
6 % 2 => 0
20 % 5 => 0
21 % 5 => 1
22 % 5 => 2
23 % 5 => 3
24 % 5 => 4
25 % 5 => 0
The % sign is for modulus, the remained when divided by. 3 % 2 = 1. So if something mod something else is zero then something is divisible by something else.