How to find the quotient of two numbers in JavaScript

青灯夜游
Release: 2022-10-09 17:46:34
Original
2779 people have browsed it

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".

How to find the quotient of two numbers in JavaScript

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

##OperatorDescriptionExample/Division operatorx / y means calculating the quotient of x divided by y

Example:

Pay attention to the division operation of special operands.

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,解释同上
Copy after login

How to find the quotient of two numbers in JavaScript

Extended knowledge: division remainder operator "%"

Remainder operation is also called modular operation. For example:

console.log(3 % 2);  //返回余数1
Copy after login

How to find the quotient of two numbers in JavaScript

Modular arithmetic mainly operates on integers, but also applies to floating point numbers. For example:

console.log(3.1 % 2.3);  //返回余数0.8000000000000003
Copy after login

How to find the quotient of two numbers in JavaScript

Example

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
Copy after login

How to find the quotient of two numbers in JavaScript

Method 2: Use the assignment operator "/="

The assignment operator is used to Assigning a value to a variable has the following two forms:

  • Simple assignment operation =: Copy the value of the operand on the right side of the equal sign directly to the operand on the left, so the operation on the left The value of the number will change.

  • Assignment operation of additional operations: Before assignment, perform some operation on the right operand, and then copy the operation result to the left operand.

The assignment operations of some additional operations can realize the four arithmetic operations of addition, subtraction, multiplication and division. Among them,

Assignment operator that implements the additional operations of the four arithmetic operations of addition, subtraction, multiplication and divisionAssignment operatorDescriptionDescriptionExampleEquivalent toDivision operation and assignmentPerform the division operation first, and then assign the result to the variable on the left side of the operatora /= ba = a / b
/=
The sample code is as follows:


var x = 50;
x /= 10;
console.log(x);  // 输出:5
Copy after login

How to find the quotient of two numbers in JavaScript

Extended knowledge: division, remainder operation and assignment%=

OperatorDescriptionExample%=First perform the modulo operation, and then assign the result to the variable on the left side of the operatorx %= y is equivalent to x = x % y
var x = 100;
x %= 15;
console.log(x);  // 输出:10
Copy after login

How to find the quotient of two numbers in JavaScript

[Related recommendations:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template