What is difference between 10/5 and 10%5 in JavaScript? Are both same ?
-
http://www.howtocreate.co.uk/tutorials/javascript/operators – Herberth Amaral Jul 26 '11 at 16:43
-
Possible duplicate of [How Does Modulus Divison Work](http://stackoverflow.com/questions/2664301/how-does-modulus-divison-work) – approxiblue Dec 02 '16 at 00:41
8 Answers
One is your basic division operation:
10/5 => 2
10/4 => 2.5
The other is the modulo operator, which will give you the integer remainder of the division operation.
10%5 => 0
10%4 => 2
- 40,178
- 28
- 107
- 146
10/5 divides 10/5 = 2
10%5 divides 5 and returns the remainder, 0, so
10%5 = 0
- 6,122
- 4
- 28
- 21
10/5 is division operation and depending on the data type storing the result in might not give you the result expected. if storing in a int you will loose the remainder.
10%2 is a modulus operation. it will return the remainder from the division and is commonly used to determine if a number is odd or even. take any given number mod 2 (N%2) and if the result is is 0 then you know the number is even.
- 592
- 3
- 8
- 25
% is the modulus operator: it gives you the remainder of the division.
- 11,168
- 5
- 47
- 61
- 28,837
- 30
- 92
- 126
10 / 5 is 10 divided by 5, or basic division.
10 % 5 is 10 modulo 5, or the remainder of a division operation.
- 6,822
- 16
- 49
- 58
- 1,209
- 8
- 17
In pretty much every language % is a modulus not a divide symbol. It does divide but it gives you just the remainder rather than the divided number.
- 630
- 3
- 8
- 20