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

概述

基本的赋值运算符是等号(=),该运算符把它右边的运算值赋给左边。即,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

加赋值(Addition assignment)

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

语法

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"

减赋值(Subtraction assignment)

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

语法

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

示例

// 假定已定义了下面的变量
//  bar = 5

bar -= 2     // 3
bar -= "foo" // NaN

乘赋值(Multiplication assignment)

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

语法

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

示例

// 假定已定义了下面的变量
//  bar = 5

bar *= 2     // 10
bar *= 'foo' // NaN

除赋值(Division assignment)

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

语法

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

示例

// 假定已定义了下面的变量
//  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

指数赋值(Exponentiation assignment)

指数赋值运算符使一个变量为底数、以右值为指数的指数运算(乘方)结果赋给该变量。更多细节查看 算术运算符

语法

语法: x **= y 
含义:  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 执行一次求值
a[i++] = a[i++] + 5 // i 执行两次求值

规范

Specification Status Comment
ECMAScript Latest Draft (ECMA-262)
Assignment operators
Draft  
ECMAScript 2015 (6th Edition, ECMA-262)
Assignment operators
Standard  
ECMAScript 5.1 (ECMA-262)
Assignment operators
Standard  
ECMAScript 1st Edition (ECMA-262)
Assignment operators
Standard Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
Addition assignment (x += y)Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
Bitwise AND assignment (x &= y)Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
Bitwise OR assignment (x |= y)Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
Bitwise XOR assignment (x ^= y)Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
Division assignment (x /= y)Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
Exponentiation assignment (x **= y)Chrome Full support 52Edge ? Firefox Full support 52IE No support NoOpera Full support YesSafari Full support 10.1WebView Android Full support 51Chrome Android Full support 52Edge Mobile ? Firefox Android Full support 52Opera Android Full support YesSafari iOS Full support 10.1Samsung Internet Android Full support 6.0nodejs Full support 7.0.0
Full support 7.0.0
Full support 6.5.0
Disabled
Disabled From version 6.5.0: this feature is behind the --harmony runtime flag.
Left shift assignment (x <<= y)Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
Multiplication assignment (x *= y)Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
Remainder assignment (x %= y)Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
Right shift assignment (x >>= y)Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
Assignment (x = y)Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
Subtraction assignment (x -= y)Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes
Unsigned right shift assignment (x >>>= y)Chrome Full support YesEdge Full support YesFirefox Full support 1IE Full support YesOpera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yesnodejs Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
User must explicitly enable this feature.
User must explicitly enable this feature.

相关链接

文档标签和贡献者

标签: 
此页面的贡献者: wbamberg, cuixiping, adoreCherish, yofine, AlexChao, SphinxKnight
最后编辑者: wbamberg,