当一个操作数除以第二个操作数时,取余运算符(%)返回剩余的余数。它与被除数的符号保持一致。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
语法
Operator: var1 % var2
示例
被除数为正数
12 % 5 // 2
1 % -2 // 1
1 % 2 // 1
2 % 3 // 2
5.5 % 2 // 1.5
被除数为负数
-12 % 5 // -2
-1 % 2 // -1
-4 % 2 // -0
被除数为NaN
NaN % 2 // NaN
规范
Specification |
---|
ECMAScript (ECMA-262) Remainder operator |
浏览器兼容性
BCD tables only load in the browser