在线文档教程

Assignment Operators

Assignment Operators

赋值运算符(assignment operator)基于右值(right operand)的值,给左值(left operand)赋值。

描述

基本的赋值运算符是等号(=),该运算符把它右边的运算值赋给左边。即,x = y 把 y的值赋给 x。 其他的赋值运算符通常是标准运算符的简写形式,如下面的定义与示例。 

NameShorthand operatorMeaning
Assignmentx = yx = y
Addition assignmentx += yx = x + y
Subtraction assignmentx -= yx = x - y
Multiplication assignmentx *= yx = x * y
Division assignmentx /= yx = x / y
Remainder assignmentx %= yx = x % y
Exponentiation assignmentx **= yx = x ** y
Left shift assignmentx <<= yx = x << y
Right shift assignmentx >>= yx = x >> y
Unsigned right shift assignmentx >>>= yx = x >>> y
Bitwise AND assignmentx &= yx = x & y
Bitwise XOR assignmentx ^= yx = x ^ y
Bitwise OR assignmentx |= yx = 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

加赋值(Addition assignment)

加赋值运算符把一个右值与一个变量相加,然后把相加的结果赋给该变量。两个操作数的类型决定了加赋值运算符的行为。算术相加或字符串连接都有可能。更多细节参考 addition operator

语法

Operator: x += y Meaning: x = x + y

示例

// Assuming the following variables // 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"

减赋值(Subtraction assignment)

减赋值运算符使一个变量减去右值,然后把结果赋给该变量。更多细节查看 subtraction operator

语法

Operator: x -= y Meaning: x = x - y

示例

// Assuming the following variable // bar = 5 bar -= 2 // 3 bar -= 'foo' // NaN

乘赋值(Multiplication assignment)

乘赋值运算符使一个变量乘以右值,然后把相成的结果赋给该变量。更多细节查看 multiplication operator

语法

Operator: x *= y Meaning: x = x * y

示例

// Assuming the following variable // bar = 5 bar *= 2 // 10 bar *= 'foo' // NaN

除赋值(Division assignment)

除赋值运算符使一个变量除以右值,然后把结果赋给该变量。更多细节查看 division operator

语法

Operator: x /= y Meaning: x = x / y

示例

// Assuming the following variable // bar = 5 bar /= 2 // 2.5 bar /= 'foo' // NaN bar /= 0 // Infinity

模赋值(Remainder assignment)

模赋值运算符使一个变量除以右值,然后把余数赋给该变量。更多细节查看 remainder operator

语法

Operator: x %= y Meaning: x = x % y

示例

// Assuming the following variable // bar = 5 bar %= 2 // 1 bar %= 'foo' // NaN bar %= 0 // NaN

指数赋值

这是一项实验技术,是ECMAScript 2016(ES7)提案的一部分。

由于该技术的规格不稳定,请查看各种浏览器的兼容性表格以供使用。还要注意,随着规范的改变,实验技术的语法和行为在未来版本的浏览器中可能会发生变化。

幂赋值运算符的计算结果为提高第一个操作数的结果功率第二个操作数。有关更多详细信息,请参见指数运算符。

语法

Operator: x **= y Meaning: x = x ** y

示例

// Assuming the following variable // bar = 5 bar **= 2 // 25 bar **= 'foo' // NaN

左移赋值(Left shift assignment)

左移赋值运算符使变量向左移动指定位数的比特位,然后把结果赋给该变量。更多细节查看 left shift operator

语法

Operator: x <<= y Meaning: x = x << y

示例

var bar = 5; // (00000000000000000000000000000101) bar <<= 2; // 20 (00000000000000000000000000010100)

右移赋值(Right shift assignment)

右移赋值运算符使变量向右移指定位数的比特位,然后把结果赋给该变量。更多细节查看 right shift operator

语法

Operator: x >>= y Meaning: x = x >> y

示例

var bar = 5; // (00000000000000000000000000000101) bar >>= 2; // 1 (00000000000000000000000000000001) var bar -5; // (-00000000000000000000000000000101) bar >>= 2; // -2 (-00000000000000000000000000000010)

无符号右移赋值(Unsigned right shift assignment)

无符号右移赋值运算符向右移动指定数量的比特位,然后把结果赋给变量。更多细节查看unsigned right shift operator

语法

Operator: x >>>= y Meaning: x = x >>> y

示例

var bar = 5; // (00000000000000000000000000000101) bar >>>= 2; // 1 (00000000000000000000000000000001) var bar = -5; // (-00000000000000000000000000000101) bar >>>= 2; // 1073741822 (00111111111111111111111111111110)

按位与赋值(Bitwise AND assignment)

按位与赋值运算符使用两个操作值的二进制表示,执行按位与运算,并把结果赋给变量。更多细节查看 bitwise AND operator

语法

Operator: x &= y Meaning: x = x & y

示例

var bar = 5; // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 bar &= 2; // 0

按位异或赋值(Bitwise XOR assignment)

按位异或赋值运算符使用两个操作值的二进制表示,执行二进制异或运算,并把结果赋给变量。更多细节查看 bitwise XOR operator

语法

Operator: x ^= y Meaning: x = x ^ y

示例

var bar = 5; bar ^= 2; // 7 // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 // ----------------------------------- // 7: 00000000000000000000000000000111

按位或赋值(Bitwise OR assignment)

按位或赋值运算符使用两个操作值的二进制表示,执行按位或运算,并把结果赋给变量。更多细节查看 bitwise OR operator

语法

Operator: x |= y Meaning: x = x | y

示例

var bar = 5; bar |= 2; // 7 // 5: 00000000000000000000000000000101 // 2: 00000000000000000000000000000010 // ----------------------------------- // 7: 00000000000000000000000000000111

示例

带有赋值运算符的左值(Left operand)

在某些不常见的情况下,赋值运算符(如 x += y)并不等同于表达式(这是是 x = x + y)。当一个赋值运算符的左值包含有一个赋值运算符时,左值只会被求值一次。例如:

a[i++] += 5 // i is evaluated only once a[i++] = a[i++] + 5 // i is evaluated twice

规范

SpecificationStatusComment
ECMAScript Latest Draft (ECMA-262)The definition of 'Assignment operators' in that specification.Living Standard
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Assignment operators' in that specification.Standard
ECMAScript 5.1 (ECMA-262)The definition of 'Assignment operators' in that specification.Standard
ECMAScript 1st Edition (ECMA-262)The definition of 'Assignment operators' in that specification.StandardInitial definition.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)

FeatureAndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)