--- title: 赋值运算符 slug: >- conflicting/Web/JavaScript/Reference/Operators_8d54701de06af40a7c984517cbe87b3e tags: - JavaScript - 运算符 translation_of: Web/JavaScript/Reference/Operators#Assignment_operators translation_of_original: Web/JavaScript/Reference/Operators/Assignment_Operators original_slug: Web/JavaScript/Reference/Operators/Assignment_Operators ---
赋值运算符(assignment operator)基于右值(right operand)的值,给左值(left operand)赋值。
本文的交互示例的源代码存储在GithHub仓库。如果你愿意贡献更多交互示例,请克隆仓库 https://github.com/mdn/interactive-examples 并提交 pull request.
基本的赋值运算符是等号(=
),该运算符把它右边的运算值赋给左边。即,x = y
把 y
的值赋给 x
。 其他的赋值运算符通常是标准运算符的简写形式,如下面的定义与示例。
名称 | 简写形式 | 含义 |
---|---|---|
赋值(Assignment) | x = y |
x = y |
加赋值(Addition assignment) | x += y |
x = x + y |
减赋值(Subtraction assignment) | x -= y |
x = x - y |
乘赋值(Multiplication assigment) | x *= y |
x = x * y |
除赋值(Division assignment) | x /= y |
x = x / y |
模赋值(Remainder assignment) | x %= y |
x = x % y |
指数赋值(Exponentiation assignment) | x **= y |
x = x ** y |
左移赋值(Left shift assignment) | x <<= y |
x = x << y |
右移赋值(Right shift assignment) | x >>= y |
x = x >> y |
无符号右移赋值(Unsigned right shift assignment) | x >>>= y |
x = x >>> y |
按位与赋值(Bitwise AND assignment) | x &= y |
x = x & y |
按位异或赋值(Bitwise XOR assignment) | x ^= y |
x = x ^ y |
按位或赋值(Bitwise OR assignment) | x |= y |
x = x | y |
简单的赋值运算符,把一个值赋给一个变量。为了把一个值赋给多个变量,可以以链式使用赋值运算符。参考下例:
Operator: x = y
// Assuming the following variables // x = 5 // y = 10 // z = 25 x = y // x is 10 x = y = z // x, y and z are all 25
加赋值运算符把一个右值与一个变量相加,然后把相加的结果赋给该变量。两个操作数的类型决定了加赋值运算符的行为。算术相加或字符串连接都有可能。更多细节参考 {{jsxref("Operators/Arithmetic_Operators", "addition operator", "#Addition", 1)}}。
Operator: x += y Meaning: x = x + y
// 定义下列变量 // foo = 'foo' // bar = 5 // baz = true // Number + Number -> addition bar += 2 // 7 // Boolean + Number -> addition baz += 1 // 2 // Boolean + Boolean -> addition baz += false // 1 // Number + String -> concatenation bar += 'foo' // "5foo" // String + Boolean -> concatenation foo += false // "foofalse" // String + String -> concatenation foo += 'bar' // "foobar"
减赋值运算符使一个变量减去右值,然后把结果赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "subtraction operator", "#Subtraction", 1)}} 。
Operator: x -= y Meaning: x = x - y
// 假定已定义了下面的变量 // bar = 5 bar -= 2 // 3 bar -= "foo" // NaN
乘赋值运算符使一个变量乘以右值,然后把相成的结果赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "multiplication operator", "#Multiplication", 1)}}。
Operator: x *= y Meaning: x = x * y
// 假定已定义了下面的变量 // bar = 5 bar *= 2 // 10 bar *= 'foo' // NaN
除赋值运算符使一个变量除以右值,然后把结果赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "division operator", "#Division", 1)}}。
Operator: x /= y Meaning: x = x / y
// 假定已定义了下面的变量 // bar = 5 bar /= 2 // 2.5 bar /= "foo" // NaN bar /= 0 // Infinity
模赋值运算符使一个变量除以右值,然后把余数赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "remainder operator", "#Remainder", 1)}}。
Operator: x %= y Meaning: x = x % y
// Assuming the following variable // bar = 5 bar %= 2 // 1 bar %= 'foo' // NaN bar %= 0 // NaN
指数赋值运算符使一个变量为底数、以右值为指数的指数运算(乘方)结果赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "算术运算符", "#Exponentiation", 1)}}。
语法: x **= y 含义: x = x ** y
// Assuming the following variable // bar = 5 bar **= 2 // 25 bar **= 'foo' // NaN
左移赋值运算符使变量向左移动指定位数的比特位,然后把结果赋给该变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "left shift operator", "#Left_shift", 1)}}。
Operator: x <<= y Meaning: x = x << y
var bar = 5; // (00000000000000000000000000000101) bar <<= 2; // 20 (00000000000000000000000000010100)
右移赋值运算符使变量向右移指定位数的比特位,然后把结果赋给该变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "right shift operator", "#Right_shift", 1)}}。
Operator: x >>= y Meaning: x = x >> y
var bar = 5; // (00000000000000000000000000000101) bar >>= 2; // 1 (00000000000000000000000000000001) var bar = -5; // (-00000000000000000000000000000101) bar >>= 2; // -2 (-00000000000000000000000000000010)
无符号右移赋值运算符向右移动指定数量的比特位,然后把结果赋给变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", " unsigned right shift operator", "#Unsigned_right_shift", 1)}}。
Operator: x >>>= y Meaning: x = x >>> y
var bar = 5; // (00000000000000000000000000000101) bar >>>= 2; // 1 (00000000000000000000000000000001) var bar = -5; // (-00000000000000000000000000000101) bar >>>= 2; // 1073741822 (00111111111111111111111111111110)
按位与赋值运算符使用两个操作值的二进制表示,执行按位与运算,并把结果赋给变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "bitwise AND operator", "#Bitwise_AND", 1)}}。
Operator: x &= y Meaning: x = x & y
var bar = 5; // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 bar &= 2; // 0
按位异或赋值运算符使用两个操作值的二进制表示,执行二进制异或运算,并把结果赋给变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "bitwise XOR operator", "#Bitwise_XOR", 1)}}。
Operator: x ^= y Meaning: x = x ^ y
var bar = 5; bar ^= 2; // 7 // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 // ----------------------------------- // 7: 00000000000000000000000000000111
按位或赋值运算符使用两个操作值的二进制表示,执行按位或运算,并把结果赋给变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "bitwise OR operator", "#Bitwise_OR", 1)}}。
Operator: x |= y Meaning: x = x | y
var bar = 5; bar |= 2; // 7 // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 // ----------------------------------- // 7: 00000000000000000000000000000111
在某些不常见的情况下,赋值运算符(如 x += y
)并不等同于表达式( x = x + y
)。当一个赋值运算符的左值包含有一个赋值运算符时,左值只会被求值一次。例如:
a[i++] += 5 // i 执行一次求值 a[i++] = a[i++] + 5 // i 执行两次求值
Specification | Status | Comment |
---|---|---|
{{SpecName('ESDraft', '#sec-assignment-operators', 'Assignment operators')}} | {{Spec2('ESDraft')}} | |
{{SpecName('ES2015', '#sec-assignment-operators', 'Assignment operators')}} | {{Spec2('ES2015')}} | |
{{SpecName('ES5.1', '#sec-11.13', 'Assignment operators')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES1', '#sec-11.13', 'Assignment operators')}} | {{Spec2('ES1')}} | Initial definition. |
{{Compat("javascript.operators.assignment")}}