-1

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

nishi
  • 377
  • 1
  • 13
  • 1
    the % means `modulo` check google for that – daremachine Oct 15 '20 at 02:11
  • [Duplicate](https://www.google.com/search?q=site%3Astackoverflow.com+js+percentage+sign) of [Understanding The Modulus Operator %](https://stackoverflow.com/q/17524673/4642212). See [What does this symbol mean in JavaScript?](https://stackoverflow.com/q/9549780/4642212) and the documentation on MDN about [expressions and operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators) and [statements](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements). – Sebastian Simon Oct 15 '20 at 02:24

3 Answers3

1

% 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.

sushitrash
  • 96
  • 1
  • 6
  • 1
    Extra nerdsauce detail: In math, the remainder and the modulo aren't exactly the same, but I think this only matters when you're working with negative numbers: https://stackoverflow.com/questions/13683563/whats-the-difference-between-mod-and-remainder (JavaScript's `%` operator returns the remainder.) – Cat Oct 15 '20 at 02:28
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.

Adrian Brand
  • 15,308
  • 3
  • 24
  • 46
0

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
Rylee
  • 696
  • 2
  • 9