Javascript 简明教程

JavaScript - Arithmetic Operators

JavaScript Arithmetic Operators

JavaScript 中的算术运算符对数字值(操作数)执行数学计算。由于对两个操作数执行计算,因此大多数算术运算符都是二元运算符。一些算术运算符是一元运算符。一元运算符对单个操作数执行计算。

Arithmetic operators in JavaScript perform mathematical calculations on numeric values (operands). Most of the arithmetic operators are binary operators as they perform calculations on two operands. Some arithmetic operators are unary operators. The unary operators perform computation on a single operand.

JavaScript 支持许多算术运算符,例如加法、减法、乘法、除法运算符等。它对算术运算符使用公共符号,例如“ + ”表示加法,“ - ”表示减法,“ * ”表示乘法,“ / ”表示除法等。

JavaScript supports many arithmetic operators such as addition, subtraction, multiplication, division operators, etc. It uses the common symbols for arithmetic operators such as "+" for addition, "-" for subtraction, "*" for multiplication, "/ " for division etc.

操作数可以是文字、变量或表达式。

The operands can be literals, variables or the expression.

var z = 3 + 5;  // 3 and 5 are literal values.
const x = 3; y = 5;
var z = x + y ; // x and y are variables.
var z = 3 + 2*x  // expression

通常,算术运算符用于执行数学运算,但它们也可用于其他运算。例如,加法运算符(+)可用于字符串连接。

In general, arithmetic operators are used to perform mathematical operations but they can be used for other operations as well. For example, the addition operator (+) can be used for string concatenation.

这里,我们给出了一个包含数学运算符并解释每个运算符功能的表格。

Here, we have given a table containing the mathematical operators and explaining the functionality of each operator.

Operator

Name

Description

+

Addition

Adds two operands

-

Subtraction

Subtracts the second operand from the first

*

Multiplication

Multiply both operands

/

Division

Divide the numerator by the denominator

%

Modulus

Outputs the remainder of an integer division

++

Increment

Increases an integer value by one

 — 

Decrement

Decreases an integer value by one

让我们借助示例来讨论不同的运算符。

Let’s discuss the different operators with the help of examples.

JavaScript Addition (+) Operator

JavaScript 加法运算符 () 将两个数字运算数相加。它以加号 () 符号表示。

The JavaScript addition () operator adds two numeric operands. It is denoted by the plus () symbol.

var x = 5, y = 10;
var sum = x + y;

此运算符还可用于连接字符串和/或数字。

This operator can also be used to concatenate strings and/or numbers.

var z = '10' + 3  // returns 103
var z = '10' + '3'  // returns 103
  1. If one operand is string, the addition operator converts the other operand to string and concatenate it with first operand.

  2. If both the operands are string, it just concatenates the second operand to the first operand.

  3. If both operands are numeric values, it will return the numeric value.

Example

在下面的示例中,我们演示了如何将两个十进制数相加,以及如何将字符串和数字串联起来。

In the below example, we demonstrate adding two decimal numbers and concatenating the strings and numbers.

<html>
<body>
   <script>
      const x = 3; y = 5;
      var z = x + y ;
      document.write(z +"</br>");
      var z = '10' + 3
      document.write(z +"</br>");
      var z = '10' + '3';
      document.write(z +"</br>");
   </script>
</body>
</html>

JavaScript Subtraction (-) Operator

JavaScript 减法运算符 (-) 将右运算数从左运算数中减去,并产生它们的差。它以减号 (-) 符号表示。

JavaScript subtraction (-) operator subtracts the right operand from the left operand and produces their difference. It is denoted by the minus (-) symbol.

20 - 10; // returns 10
'20' - 10; // returns 10
'20' - '10'; // returns 10
'20ee' - 10; // returns NaN
NaN - 10 // return NaNs
Infinity - 10 // returns infinity
  1. The subtraction operator uses numeric operands but can also be used for non-numeric operands such as strings.

  2. If both operands are numbers, then resultant is number.

  3. If any or both operands are strings (containing only numbers), it first converts the strings to number and then performs subtraction operations.

  4. If string contains non numeric value, it will return NaN.

  5. If any operand is NaN or Infinity, the result will be NaN or Infinity respectively.

Example

在下面的示例中,我们演示了如何将两个十进制数相减,以及其他数据类型的减法。

In the below example, we demonstrate the subtraction two decimal numbers and of other datatypes.

<html>
<body>
   <script>
      var x = 20; y = 10;
      var z = x - y ;
      document.write(z +"</br>");
      x = "20"; y = "10"
      z = x - y ;
      document.write(z +"</br>");
      x = "20ee";
      z = x - y ;
      document.write(z +"</br>");
   </script>
   <p>Change the values of the variables and test the resultant values</p>
</body>
</html>

JavaScript Multiplication (*) Operator

JavaScript 乘法运算符乘以两个数字(运算数)。它给出两个运算数的乘积。它以星号 (*) 符号表示。如果两个运算数具有相同符号,则乘积为正数。如果两个运算数具有不同的符号,则乘积为负数。

The JavaScript multiplication operator multiplies two numbers (operands). It gives the product of two operands. It is denoted by the asterisk (*) symbol. If two operands are of same sign, the product is positive. If the two operands are of different sign, the product is negative.

如果任何一个或两个运算数是字符串,则它会将字符串转换为数字,然后返回它们的乘积。

If any or both operands are string, it converts the string to number and then returns their product.

Example

在下面的示例中,我们演示了如何对不同类型的运算数使用乘法运算符。

In the example below, we demonstrate the use of multiplication operator on different types of operands.

<html>
<body>
   <script>
      var x = 20; y = 10;
      var z = x * y ;
      document.write(z +"</br>");
      x = "20"; y = "10"
      z = x * y ;
      document.write(z +"</br>");
      x = "20ee";
      z = x * y ;
      document.write(z +"</br>");
   </script>
   <p>Change the values of the variables and test the resultant values</p>
</body>
</html>

JavaScript Division (/) Operator

JavaScript 运算符 (/) 将左操作数(被除数)除以右操作数(除数)并返回商数。它用斜杠 (/) 符号表示。

The JavaScript division (/) operator divides the left operand (dividend) by the right operand (divisor) and returns the quotient. It is represented by the slash (/) symbol.

20/10  // returns 2
20/-10 // return -2
100/0  // returns Infinity
0/0    // returns NaN

Example

我们来演示一下除法运算符的使用。

Let’s demonstrate the use of division operator.

<html>
<body>
   <script>
      var x = 20; y = 10;
      var z = x / y ;
      document.write(z +"</br>");
      x = "20"; y = "10"
      z = x / y ;
      document.write(z +"</br>");
      z = x / 0 ;
      document.write(z +"</br>");
      z = 0 / 0 ;
      document.write(z +"</br>");
   </script>
   <p>Change the values of the variables and test the resultant values</p>
</body>
</html>

JavaScript Modulus (%) Operator

JavaScript 取模运算符 (%) 会在第一个操作数除以第二个操作数时返回余数。它又称为余数运算符。它用百分号 (%) 符号表示。它采用被除数的符号。我们举个例子:5%3 等于 2,因为 5 除以 3 时余数是 2。

The JavaScript modulus (%) operator returns the remainder when first operand is divided by the second operand. It is also known as remainder operator. It is denoted by the percent (%) symbol. It takes the sign of dividend. Let’s take an example 5%3 gives 2 because when 5 is divided by 3, it gives remainder as 2.

Example

我们通过一个示例程序来理解取模运算符。

Let’s understand the modulus operator with the help of an example program.

<html>
<body>
   <script>
      var x = 20 % 9;
      var y = -20 % 9;
      var z = 20.43 % 9;
      var a = 20 % -9;
      var b = 20 % 10;
      document.write(x +"</br>");
      document.write(y +"</br>");
      document.write(z +"</br>");
      document.write(a +"</br>");
      document.write(b +"</br>");
   </script>
  </body>
</html>

JavaScript Increment (++) Operator

JavaScript 自增运算符 () 将操作数的值增加 1。它是一个一元运算符。它只接受一个操作数。它用双加号 () 符号表示。

The JavaScript increment () operator increases the value of operand by one. It is an unary operator. It takes only one operand. It is denoted by double plus () sign.

JavaScript 中共有两种类型的自增运算符 −

There are two types of increment operator in JavaScript −

Prefix Increment Operator

前缀自增运算符在使用变量的当前值之前将变量的值增加 1。例如:

The prefix increment operator increments the value of the variable before its current value is used. For example,

var x = 10;
var y = ++x; // x is now 11 and y is also 11.

Postfix Increment Operator

后缀自增运算符在使用变量的当前值之后将变量的值增加 1。例如:

The postfix increment operator increments the value of the variable after its current value is used. For example,

var a = 10;
var b = a++; // a is now 11 but b is 10.

这里,在以上代码的第二行中,首先将 a 的当前值赋值给 b,然后对其进行自增。

Here, in the second line of the above code, first the current value of a is assigned to b, then it is incremented.

我们来看以下示例 −

Let’s look at the following example −

<html>
<body>
   <script>
      var x = 10;
      var y = --x;  //prefix decrement
      var a = 10;
      var b = a--;  // postfix decrement
	  document.write("x = " + x);
      document.write(" y = " + y + "<br>");
      document.write("a = " + a);
      document.write(" b = " + b + "<br>");
   </script>
   <p>Change the values of the variables and check the results</p>
</body>
</html>

JavaScript Decrement (--) Operator

JavaScript 自减运算符 (--) 将操作数的值减少 1。它也是一个一元运算符,即它只接受一个操作数。它用双减号 (--) 符号表示。

The JavaScript decrement (--) operator decreases the value of operand by one. It is also an unary operator, i.e., it takes only one operand. It is denoted by double minus (--) sign.

JavaScript 中中共两种类型的自减运算符 −

There are two types of decrement operator in JavaScript −

Prefix Decrement Operator

前缀自减运算符在使用变量的当前值之前将变量的值减少 1。例如:

The prefix decrement operator decrements the value of the variable before its current value is used. For example,

var x = 10;
var y = --x; // x is now 9 and y is also 9.

Postfix Decrement Operator

后缀自减运算符在使用变量的当前值之后将变量的值减少 1。例如:

The postfix decrement operator decrements the value of the variable after its current value is used. For example,

var a = 10;
var b = a--; // a is now 9 but b is 10.

这里,在以上代码的第二行中,首先将 a 的当前值赋值给 b,然后对其进行自减。

Here, in the second line of the above code, first the current value of a is assigned to b, then it is decremented.

我们来看以下示例 −

Let’s look at the following example −

<html>
<body>
   <script>
      var x = 10;
      var y = --x;  //prefix decrement
      var a = 10;
      var b = a--;  // postfix decrement
	  document.write("x = " + x);
      document.write(" y = " + y + "<br>");
      document.write("a = " + a);
      document.write(" b = " + b + "<br>");
   </script>
   <p>Change the values of the variables and check the results</p>
</body>
</html>