What does % do in JavaScript?

dakabfancy提出了一个问题:What does % do in JavaScript?,或许与您遇到的问题类似。

回答者T.J. Crowdernulltoken给出了该问题的处理方式:

It's a modulo operator. See this documentation or the specification for more information on JavaScript arithmetic operators.

% (Modulus)

The modulus operator is used as follows:

var1 % var2

The modulus operator returns the first operand modulo the second operand, that is, var1 modulo var2, in the preceding statement, where var1 and var2 are variables. The modulo function is the integer remainder of dividing var1 by var2. For example, 12 % 5 returns 2. The result will have the same sign as var1; that is, −1 % 2 returns −1.



欢迎 JavaScript 爱好者