Javascript 简明教程

JavaScript - Assignment Operators

JavaScript Assignment Operators

JavaScript 中的 assignment 运算符用于向变量赋值。这些是二元运算符。赋值运算符采用两个操作数,根据右操作数的值向左操作数赋值。左操作数始终是变量,右操作数可以是文字、变量或表达式。

The assignment operators in JavaScript are used to assign values to the variables. These are binary operators. An assignment operator takes two operands, assigns a value to the left operand based on the value of right operand. The left operand is always a variable and the right operand may be literal, variable or expression.

let x = 10; // right operand is a literal
let y = x; // right operand is a variable
let z = x + 10; // right operand is an expression

assignment 操作符首先计算表达式,然后将值赋给变量(左操作数)。

An assignment operator first evaluates the expression and then assign the value to the variable (left operand).

一个简单的赋值运算符是 equal (=) 运算符。在 JavaScript 语句“let x = 10;”中,= 运算符将 10 赋给变量 x。

A simple assignment operator is equal (=) operator. In the JavaScript statement "let x = 10;", the = operator assigns 10 to the variable x.

我们可以将一个简单的赋值运算符与算术、逻辑等其他类型的运算符组合来获得复合赋值运算符。一些算术赋值运算符是 =、-=、*=、/= 等。= 运算符对操作数执行加法运算,并将结果赋给左手操作数。

We can combine a simple assignment operator with other type of operators such as arithmetic, logical, etc. to get compound assignment operators. Some arithmetic assignment operators are +=, -=, *=, /=, etc. The += operator performs addition operation on the operands and assign the result to the left hand operand.

Arithmetic Assignment Operators

在本节中,我们将介绍简单的赋值和算术赋值运算符。算术赋值运算符执行算术运算,并将结果赋给变量。以下是带有示例的运算符列表 -

In this section, we will cover simple assignment and arithmetic assignment operators. An arithmetic assignment operator performs arithmetic operation and assign the result to a variable. Following is the list of operators with example −

Assignment Operator

Example

Equivalent To

= (Assignment)

a =

a = b

+= (Addition Assignment)

a +=

a = a + b

-= (Subtraction Assignment)

a -=

a = a – b

*= (Multiplication Assignment)

a *=

a = a * b

/= (Division Assignment)

a /=

a = a / b

%= (Remainder Assignment)

a %=

a = a % b

**= (Exponentiation Assignment)

a **=

a = a ** b

Simple Assignment (=) Operator

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 5;
    let y = x + 10;
    document.getElementById("output").innerHTML =
    "Value of x : " + x + "<br>" +
    "Value of y : " + y;
  </script>
</body>
</html>

以下是赋值链的示例 -

Below is an example of assignment chaining −

<html>
<body>
  <div id="output"></div>
  <script>
    let x = y = 5;
    document.getElementById("output").innerHTML =
    "Value of x : " + x + "<br>" +
    "Value of y : " + y;
  </script>
</body>
</html>

Addition Assignment (+=) Operator

JavaScript 加法赋值运算符在两个操作数上执行加法,并将结果赋给左操作数。在加法中可能是数字加法或字符串连接。

The JavaScript addition assignment operator performs addition on the two operands and assigns the result to the left operand. Here addition may be numeric addition or string concatenation.

x += b;

在上面的语句中,它添加了 b 和 x 的值,并将结果赋给 x。

In the above statement, it adds values of b and x and assigns the result to x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 5;
    x += 7;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>
<html>
<body>
  <div id="output"></div>
  <script>
    let x ="Hello";
    x += " World";
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Subtraction Assignment (-=) Operator

JavaScript 中的减法赋值运算符从左操作数中减去右操作数的值,并将结果赋给左操作数(变量)。

The subtraction assignment operator in JavaScript subtracts the value of right operand from the left operand and assigns the result to left operand (variable).

let x -=b;

在上面的语句中,它从 x 减去 b 并将结果分配给 x。

In the above statement, it subtracts b from x and assigns the result to x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 15;
    x -= 5;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Multiplication Assignment (*=) Operator

JavaScript 中的乘法赋值运算符对两个操作数进行乘法运算,并将其结果分配给左操作数。

The multiplication assignment operator in JavaScript multiplies the both operands and assign the result to the left operand.

let x *= b;

在上面的语句中,它对 x 和 b 进行乘法运算,并将其结果分配给 x。

In the above statement, it multiplies x and b and assigns the result to x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 10;
	x *= 5;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Division Assignment (/=) Operator

此运算符将左操作数除以右操作数,并将结果分配给左操作数。

This operator divides left operand by the right operand and assigns the result to left operand.

let x /= b;

在上面的语句中,它将 x 除以 b,并将结果(商)分配给 x。

In the above statement, it divides x by b and assigns the result (quotient) to x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 10;
    x /= 5;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Remainder Assignment (%=) Operator

JavaScript 求余赋值运算符对操作数进行求余运算,并将其结果分配给左操作数。

The JavaScript remainder assignment operator performs the remainder operation on the operands and assigns the result to left operand.

let x %= b;

在上面的语句中,它将 x 除以 b,并将结果(余数)分配给 x。

In the above statement, it divides x by b and assigns the result (remainder) to x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 12;
    x %= 5;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Exponentiation Assignment (**=) Operator

此运算符对操作数执行幂运算,并将结果分配给左操作数。

This operator performs exponentiation operation on the operands and assigns the result to left operand.

let x **= b;

在上面的语句中,它计算 x**b 并将其结果分配给 x。

In the above statement, it computes x**b and assigns the result to x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 5;
    x **= 3;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

JavaScript Bitwise Assignment operators

按位赋值运算符对操作数执行按位运算,并将结果分配给变量。这些运算执行两个运算:首先是按位运算,其次是简单的赋值运算。按位运算是在二进制位级别执行的。按位运算符将两个操作数都作为 32 位有符号整数对待,并对操作数的相应位执行运算。简单的赋值运算符将结果分配给变量(左操作数)。

A bitwise assignment operator performs bitwise operation on the operands and assign the result to a variable. These operations perform two operations, first a bitwise operation and second the simple assignment operation. Bitwise operation is done on bit-level. A bitwise operator treats both operands as 32-bit signed integers and perform the operation on corresponding bits of the operands. The simple assignment operator assigns result is to the variable (left operand).

以下是带示例的运算符列表:

Following is the list of operators with example −

Assignment Operator

Example

Equivalent To

&= (Bitwise AND Assignment)

a &=

a = a & b

= (Bitwise OR Assignment)

a

=

a = a

b

^= (Bitwise XOR Assignment)

a ^=

a = a ^ b

Bitwise AND Assignment Operator

JavaScript 按位 AND 赋值 (&=) 运算符对操作数执行按位 AND 运算,并将结果分配给左操作数(变量)。

The JavaScript bitwise AND assignment (&=) operator performs bitwise AND operation on the operands and assigns the result to the left operand (variable).

let x &= b;

在上面的语句中,它对 x 和 b 进行按位 AND 运算,并将结果分配给变量 x。

In the above statement, it performs bitwise AND on x and b and assigns the result to the variable x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 7;
    x &= 5;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Bitwise OR Assignment Operator

JavaScript 按位 OR 赋值 (|=) 运算符对操作数执行按位 OR 运算,并将结果分配给左操作数(变量)。

The JavaScript bitwise OR assignment (|=) operator performs bitwise OR operation on the operands and assigns the result to the left operand (variable).

let x |= b;

在上面的语句中,它对 x 和 b 进行按位 OR 运算,并将结果分配给变量 x。

In the above statement, it performs bitwise OR on x and b and assigns the result to the variable x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 7;
    x |= 5;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Bitwise XOR Assignment Operator

JavaScript 按位 XOR 赋值 (^=) 运算符对操作数执行按位 XOR 运算,并将结果分配给左操作数(变量)。

The JavaScript bitwise XOR assignment (^=) operator performs bitwise XOR operation on the operands and assigns the result to the left operand (variable).

let x ^= b;

在上面的语句中,它对 x 和 b 进行按位 XOR 运算,并将结果分配给变量 x。

In the above statement, it performs bitwise XOR on x and b and assigns the result to the variable x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 7;
    x ^= 5;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

JavaScript Shift Assignment Operators

移位赋值运算符对操作数执行按位移位操作,并将结果赋值给变量(左操作数)。这些是两个运算符的组合,第一个是按位移位运算符,第二个是简单的赋值运算符。

A shift assignment operator performs bitwise shift operation on the operands and assign the result to a variable (left operand). These are a combinations two operators, the first bitwise shift operator and second the simple assignment operator.

以下是移位赋值运算符的列表,带示例:

Following is the list of the shift assignment operators with example −

Assignment Operator

Example

Equivalent To

<⇐ (Left Shift Assignment)

a <⇐

a = a << b

>>= (Right Shift Assignment)

a >>=

a = a >> b

>>>= (Unsigned Right Shift Assignment)

a >>>=

a = a >>> b

Left Shift Assignment Operator

JavaScript 左移位赋值运算符 (<⇐) 对操作数执行左移位操作,并将结果赋值给左操作数(变量)。

The JavaScript left shift assignment (<⇐) operator performs left shift operation on the operands and assigns the result to the left operand (variable).

let x <<= b;

在上面的语句中,它对 x 和 b 执行左移位,并将结果赋值给变量 x。

In the above statement, it performs left shift on x and b and assigns the result to the variable x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 7;
    x <<= 2;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Right Shift Assignment Operator

JavaScript 右移位赋值运算符 (>>=) 对操作数执行右移位操作,并将结果赋值给左操作数(变量)。

The JavaScript right shift assignment (>>=) operator performs right shift operation on the operands and assigns the result to the left operand (variable).

let x >>= b;

在上面的语句中,它对 x 和 b 执行右移位,并将结果赋值给变量 x。

In the above statement, it performs right shift on x and b and assigns the result to the variable x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 7;
	x >>= 1;
    document.getElementById("output").innerHTML = "Value of x : " + x;
  </script>
</body>
</html>

Unsigned Right Shift Assignment Operator

JavaScript 无符号右移位赋值运算符 (>>>=) 对操作数执行无符号右移位操作,并将结果赋值给左操作数(变量)。

The JavaScript unsigned right shift assignment (>>>=) operator performs unsigned right shift operation on the operands and assigns the result to the left operand (variable).

let x >>>= b;

在上面的语句中,它对 x 和 b 执行无符号右移位,并将结果赋值给变量 x。

In the above statement, it performs unsigned right shift on x and b and assigns the result to the variable x.

<html>
<body>
  <div id="output"></div>
  <script>
    let x = 7;
    x >>>= 2;
    document.getElementById("output").innerHTML ="Value of x : " + x;
  </script>
</body>
</html>

JavaScript Logical Assignment operators

在 JavaScript 中,逻辑赋值运算符对操作数执行逻辑运算,并将结果赋值给变量(左操作数)。每个逻辑赋值运算符都是两个运算符的组合,第一个是逻辑运算符,第二个是简单的赋值运算符。

In JavaScript, a logical assignment operator performs a logical operation on the operands and assign the result to a variable (left operand). Each logical assignment operator is a combinations two operators, the first logical operator and second the simple assignment operator.

以下是逻辑赋值运算符的列表,带示例:

Following is the list of the logical assignment operators with example −

Assignment Operator

Example

Equivalent To

&&= (Logical AND Assignment)

a &&=

a = a && b

= (Logical OR Assignment)

a

=

a = a

b

??= (Nullish Coalescing Assignment)

a ??=

a = a ?? b

Example

<html>
<body>
  <div id="output"></div>
  <script>
    var a = 5;
    var b = 10;
    var result = (a &&= b);
    document.getElementById("output").innerHTML =
	"Value of (a &&= b) => " + result + "<br/>";
    result = (a ||= b);
    document.getElementById("output").innerHTML +=
	"Value of (a ||= b) => " + result;
  </script>
</body>
</html>