Home > Article > Web Front-end > Does javascript support the method of finding the remainder?
Javascript supports the method of finding the remainder. You can use the "%" operator to find the remainder; the "%" operator can divide the values on both sides of the operator and obtain the remainder of the division between the two. This operation The operator mainly operates on integers, but also applies to floating point numbers. The syntax is "divisor% dividend".
The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.
javascript supports finding the remainder
1. You can use modular operation to find the remainder through the modulo operator "%" . Example x = y % 2.
console.log(3 % 2); //返回余数1
2. Modulo arithmetic 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.
Examples are as follows:
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
Expanded knowledge of JavaScript arithmetic operators:
[Related recommendations: javascript video tutorial、webfrontend】
The above is the detailed content of Does javascript support the method of finding the remainder?. For more information, please follow other related articles on the PHP Chinese website!