What is the % operator in JavaScript? [duplicate]

蓝咒 提交于 2019-12-14 03:29:56

问题


I am trying to decipher a piece of JS code (while (obviously) my JS knowledge is null).
What is the % operator in the code below?

   m[p1%2][q1] = 0.0;  
   for(j=q1+1; j <= q2; j++)
      m[p1%2][j] = m[p1%2][j-1] + 1;

回答1:


% is remainder operator. It gives the remainder after division. Quoting from MDN

The remainder operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend, not the divisor. It uses a built-in modulo function to produce the result, which is the integer remainder of dividing var1 by var2 — for example — var1 modulo var2.

There is a proposal to get an actual modulo operator in a future version of ECMAScript, the difference being that the modulo operator result would take the sign of the divisor, not the dividend.




回答2:


% is modulo (or modulus?). It divides the first term by the second and returns the remainder. This can be useful for looping through a set of numbers and many other things.

Usage: x % y

Examples:

5 % 10; //5
10 % 5; //0
16 % 5; //1



回答3:


% is a Mathematical operator, it is used for Modulus. in simple 3 % 2=1 Modulus operator returns reminder.



来源:https://stackoverflow.com/questions/37005330/what-is-the-operator-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!