-2

What is difference between 10/5 and 10%5 in JavaScript? Are both same ?

Pratik
  • 11,534
  • 22
  • 69
  • 99
Jitendra Vyas
  • 148,487
  • 229
  • 573
  • 852

8 Answers8

5
10/5 = 2
10%5 = 0

% is modulo

James Montagne
  • 77,516
  • 14
  • 110
  • 130
3

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
Matt
  • 41,216
  • 30
  • 109
  • 147
1

10/5 divides 10/5 = 2
10%5 divides 5 and returns the remainder, 0, so
10%5 = 0

velcrow
  • 6,336
  • 4
  • 29
  • 21
1

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.

sauce
  • 592
  • 4
  • 9
  • 25
0

http://www.w3schools.com/js/js_operators.asp

10 / 5 is 2. 10 % 5 is 0.

jbabey
  • 45,965
  • 12
  • 71
  • 94
0

% is the modulus operator: it gives you the remainder of the division.

Adrian Toman
  • 11,316
  • 5
  • 48
  • 62
sushil bharwani
  • 29,685
  • 30
  • 94
  • 128
0

10 / 5 is 10 divided by 5, or basic division.

10 % 5 is 10 modulo 5, or the remainder of a division operation.

approxiblue
  • 6,982
  • 16
  • 51
  • 59
squiddle
  • 1,259
  • 8
  • 17
0

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.

Gagege
  • 650
  • 3
  • 8
  • 20