In JavaScript, you can use the modulo operator "%" to find the remainder, the syntax is "a % b"; the modulo operator mainly performs remainder operations on integers, but it is also applicable to floating point numbers, for example "3.1 % 2.3".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In javascript, you can use modular arithmetic to find the remainder through the modulo operator "%". Example x = y % 2
console.log(3 % 2); //返回余数1
Modulo operation mainly operates on integers, but also applies to floating point numbers. For example:
console.log(3.1 % 2.3); //返回余数0.8000000000000003
Pay attention to the remainder operation of special operands.
var n = 5; //定义并初始化任意一个数值 console.log(Infinity % n); //返回NaN console.log(Infinity % Infinity); //返回NaN console.log(n % Infinity); //返回5 console.log(0 % n); //返回0 console.log(0 % Infinity); //返回0 console.log(n % 0); //返回NaN console.log(Infinity % 0); //返回NaN
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to find the remainder in javascript. For more information, please follow other related articles on the PHP Chinese website!