开发者

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

What is difference bet开发者_高级运维ween 10/5 and 10%5 in JavaScript? Are both same ?


10/5 = 2
10%5 = 0

% is modulo


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


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.


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


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

10 / 5 is 2. 10 % 5 is 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.


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


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜