赋值运算符(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)

该特性目前仍处于 ECMAScript 7 规范提案中
目前的实现在未来可能会发生改变,甚至被完全删除,请谨慎使用。

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

语法

语法: 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.

浏览器兼容性

FeatureChromeEdgeFirefoxInternet ExplorerOperaSafari
Addition assignment (x += y) Yes Yes1 Yes Yes Yes
Bitwise AND assignment (x &= y) Yes Yes1 Yes Yes Yes
Bitwise OR assignment (x |= y) Yes Yes1 Yes Yes Yes
Bitwise XOR assignment (x ^= y) Yes Yes1 Yes Yes Yes
Division assignment (x /= y) Yes Yes1 Yes Yes Yes
Exponentiation assignment (x **= y)52 ?52 No Yes10.1
Left shift assignment (x <<= y) Yes Yes1 Yes Yes Yes
Multiplication assignment (x *= y) Yes Yes1 Yes Yes Yes
Remainder assignment (x %= y) Yes Yes1 Yes Yes Yes
Right shift assignment (x >>= y) Yes Yes1 Yes Yes Yes
Assignment (x = y) Yes Yes1 Yes Yes Yes
Subtraction assignment (x -= y) Yes Yes1 Yes Yes Yes
Unsigned right shift assignment (x >>>= y) Yes Yes1 Yes Yes Yes
FeatureAndroid webviewChrome for AndroidEdge mobileFirefox for AndroidOpera AndroidiOS SafariSamsung Internet
Addition assignment (x += y) Yes Yes Yes4 Yes Yes Yes
Bitwise AND assignment (x &= y) Yes Yes Yes4 Yes Yes Yes
Bitwise OR assignment (x |= y) Yes Yes Yes4 Yes Yes Yes
Bitwise XOR assignment (x ^= y) Yes Yes Yes4 Yes Yes Yes
Division assignment (x /= y) Yes Yes Yes4 Yes Yes Yes
Exponentiation assignment (x **= y)5152 ?52 Yes10.16.0
Left shift assignment (x <<= y) Yes Yes Yes4 Yes Yes Yes
Multiplication assignment (x *= y) Yes Yes Yes4 Yes Yes Yes
Remainder assignment (x %= y) Yes Yes Yes4 Yes Yes Yes
Right shift assignment (x >>= y) Yes Yes Yes4 Yes Yes Yes
Assignment (x = y) Yes Yes Yes4 Yes Yes Yes
Subtraction assignment (x -= y) Yes Yes Yes4 Yes Yes Yes
Unsigned right shift assignment (x >>>= y) Yes Yes Yes4 Yes Yes Yes

相关链接

文档标签和贡献者

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