Home > Article > Web Front-end > How to find the quotient of two numbers in JavaScript
Two implementation methods: 1. Use the arithmetic operator "/" and the syntax "operand 1 / operand 2" to divide the operands on both sides of the operator and return the quotient; 2. Use The assignment operator "/=" will first perform a division operation, and then assign the result to the variable on the left side of the operator. The syntax "x /= y" is equivalent to "x = x / y".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In JavaScript, you can use the "/" or "/=" operator to find the quotient of two numbers.
Method 1: Use the arithmetic operator "/"
Arithmetic operators are used to perform common mathematical operations, such as addition, subtraction, Multiplication, division, etc. Where
Description | Example | |
---|---|---|
Division operator | x / y means calculating the quotient of x divided by y |
Example:
var n = 5; //定义并初始化任意一个数值 console.log(NaN / n); //如果一个操作数是NaN,结果都是NaN console.log(Infinity / n); //Infinity被任意数字除,结果是Infinity或-Infinity //符号由第二个操作数的符号决定 console.log(Infinity / Infinity); //返回NaN console.log(n / 0); //0除一个非无穷大的数字,结果是Infinity或-Infinity,符号由第二个操作数的符号决定 console.log(n / -0); //返回-Infinity,解释同上
Extended knowledge: division remainder operator "%"
Remainder operation is also called modular operation. For example:console.log(3 % 2); //返回余数1Modular arithmetic mainly operates on integers, but also applies to floating point numbers. For example:
console.log(3.1 % 2.3); //返回余数0.8000000000000003ExamplePay 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
Method 2: Use the assignment operator "/="
The assignment operator is used to Assigning a value to a variable has the following two forms:Description | Description | Example | Equivalent to | |
---|---|---|---|---|
/=
| Division operation and assignmentPerform the division operation first, and then assign the result to the variable on the left side of the operator | a /= b | a = a / b |
var x = 50; x /= 10; console.log(x); // 输出:5
Extended knowledge: division, remainder operation and assignment%=
Description | Example | |
---|---|---|
First perform the modulo operation, and then assign the result to the variable on the left side of the operator | x %= y is equivalent to x = x % y |
Basic Programming Video]
The above is the detailed content of How to find the quotient of two numbers in JavaScript. For more information, please follow other related articles on the PHP Chinese website!