JavaScript 中的 % 运算符是什么?
What is the % operator in JavaScript?
我正在尝试破译一段 JS 代码(而(显然)我的 JS 知识是空的)。
下面代码中的 % 运算符是什么?
m[p1%2][q1] = 0.0;
for(j=q1+1; j <= q2; j++)
m[p1%2][j] = m[p1%2][j-1] + 1;
%
是 remainder operator。它给出除法后的余数。
引用自 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.
% 是模数(或模数?)。它将第一项除以第二项,余数 returns。这对于循环遍历一组数字和许多其他内容很有用。
用法:x % y
示例:
5 % 10; //5
10 % 5; //0
16 % 5; //1
% 是数学运算符,用于模数。
简而言之 3 % 2=1
取模运算符returns提醒.
我正在尝试破译一段 JS 代码(而(显然)我的 JS 知识是空的)。
下面代码中的 % 运算符是什么?
m[p1%2][q1] = 0.0;
for(j=q1+1; j <= q2; j++)
m[p1%2][j] = m[p1%2][j-1] + 1;
%
是 remainder operator。它给出除法后的余数。
引用自 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
byvar2
— 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.
% 是模数(或模数?)。它将第一项除以第二项,余数 returns。这对于循环遍历一组数字和许多其他内容很有用。
用法:x % y
示例:
5 % 10; //5
10 % 5; //0
16 % 5; //1
% 是数学运算符,用于模数。 简而言之 3 % 2=1 取模运算符returns提醒.