1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
---
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
---
<div>{{jsSidebar("Operators")}}</div>
<p>赋值运算符(<strong>assignment operator</strong>)基于右值(right operand)的值,给左值(left operand)赋值。</p>
<div>{{EmbedInteractiveExample("pages/js/expressions-assignment.html")}}</div>
<p class="hidden">本文的交互示例的源代码存储在GithHub仓库。如果你愿意贡献更多交互示例,请克隆仓库 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> 并提交 pull request.</p>
<h2 id="概述">概述</h2>
<p>基本的赋值运算符是等号(<code>=</code>),该运算符把它右边的运算值赋给左边。即,<code>x = y</code> 把 <code>y</code> 的值赋给 <code>x</code>。 其他的赋值运算符通常是标准运算符的简写形式,如下面的定义与示例。 </p>
<table class="standard-table">
<tbody>
<tr>
<th>名称</th>
<th>简写形式</th>
<th>含义</th>
</tr>
<tr>
<td><a href="#Assignment">赋值(Assignment)</a></td>
<td><code>x = y</code></td>
<td><code>x = y</code></td>
</tr>
<tr>
<td><a href="#Addition_assignment">加赋值(Addition assignment)</a></td>
<td><code>x += y</code></td>
<td><code>x = x + y</code></td>
</tr>
<tr>
<td><a href="#Subtraction_assignment">减赋值(Subtraction assignment)</a></td>
<td><code>x -= y</code></td>
<td><code>x = x - y</code></td>
</tr>
<tr>
<td><a href="#Multiplication_assignment">乘赋值(Multiplication assigment)</a></td>
<td><code>x *= y</code></td>
<td><code>x = x * y</code></td>
</tr>
<tr>
<td><a href="#Division_assignment">除赋值(Division assignment)</a></td>
<td><code>x /= y</code></td>
<td><code>x = x / y</code></td>
</tr>
<tr>
<td><a href="#Remainder_assignment">模赋值(Remainder assignment)</a></td>
<td><code>x %= y</code></td>
<td><code>x = x % y</code></td>
</tr>
<tr>
<td><a href="#Exponentiation_assignment">指数赋值(Exponentiation assignment)</a></td>
<td><code>x **= y</code></td>
<td><code>x = x ** y</code></td>
</tr>
<tr>
<td><a href="#Left_shift_assignment">左移赋值(Left shift assignment)</a></td>
<td><code>x <<= y</code></td>
<td><code>x = x << y</code></td>
</tr>
<tr>
<td><a href="#Right_shift_assignment">右移赋值(Right shift assignment)</a></td>
<td><code>x >>= y</code></td>
<td><code>x = x >> y</code></td>
</tr>
<tr>
<td><a href="#Unsigned_right_shift_assignment">无符号右移赋值(Unsigned right shift assignment)</a></td>
<td><code>x >>>= y</code></td>
<td><code>x = x >>> y</code></td>
</tr>
<tr>
<td><a href="#Bitwise_AND_assignment">按位与赋值(Bitwise AND assignment)</a></td>
<td><code>x &= y</code></td>
<td><code>x = x & y</code></td>
</tr>
<tr>
<td><a href="#Bitwise_XOR_assignment">按位异或赋值(Bitwise XOR assignment)</a></td>
<td><code>x ^= y</code></td>
<td><code>x = x ^ y</code></td>
</tr>
<tr>
<td><a href="#Bitwise_OR_assignment">按位或赋值(Bitwise OR assignment)</a></td>
<td><code>x |= y</code></td>
<td><code>x = x | y</code></td>
</tr>
</tbody>
</table>
<h2 id="赋值"><a name="Assignment">赋值</a></h2>
<p>简单的赋值运算符,把一个值赋给一个变量。为了把一个值赋给多个变量,可以以链式使用赋值运算符。参考下例:</p>
<h4 id="语法">语法</h4>
<pre class="syntaxbox"><strong>Operator:</strong> x = y
</pre>
<h4 id="示例">示例</h4>
<pre class="brush: js">// 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
</pre>
<h3 id="加赋值(Addition_assignment)"><a name="Addition_assignment">加赋值(Addition assignment)</a></h3>
<p>加赋值运算符把一个右值与一个变量相加,然后把相加的结果赋给该变量。两个操作数的类型决定了加赋值运算符的行为。算术相加或字符串连接都有可能。更多细节参考 {{jsxref("Operators/Arithmetic_Operators", "addition operator", "#Addition", 1)}}。</p>
<h4 id="语法_2">语法</h4>
<pre class="syntaxbox"><strong>Operator:</strong> x += y
<strong>Meaning:</strong> x = x + y
</pre>
<h4 id="示例_2">示例</h4>
<pre class="brush: js">// 定义下列变量
// 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"
</pre>
<h3 id="减赋值(Subtraction_assignment)"><a name="Subtraction_assignment">减赋值(Subtraction assignment)</a></h3>
<p>减赋值运算符使一个变量减去右值,然后把结果赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "subtraction operator", "#Subtraction", 1)}} 。</p>
<h4 id="语法_3">语法</h4>
<pre class="syntaxbox"><strong>Operator:</strong> x -= y
<strong>Meaning:</strong> x = x - y
</pre>
<h4 id="示例_3">示例</h4>
<pre class="brush: js">// 假定已定义了下面的变量
// bar = 5
bar -= 2 // 3
bar -= "foo" // NaN
</pre>
<h3 id="乘赋值(Multiplication_assignment)"><a name="Multiplication_assignment">乘赋值(Multiplication assignment)</a></h3>
<p>乘赋值运算符使一个变量乘以右值,然后把相成的结果赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "multiplication operator", "#Multiplication", 1)}}。</p>
<h4 id="语法_4">语法</h4>
<pre class="syntaxbox"><strong>Operator:</strong> x *= y
<strong>Meaning:</strong> x = x * y
</pre>
<h4 id="示例_4">示例</h4>
<pre class="brush: js">// 假定已定义了下面的变量
// bar = 5
bar *= 2 // 10
bar *= 'foo' // NaN
</pre>
<h3 id="除赋值(Division_assignment)"><a name="Division_assignment">除赋值(Division assignment)</a></h3>
<p>除赋值运算符使一个变量除以右值,然后把结果赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "division operator", "#Division", 1)}}。</p>
<h4 id="语法_5">语法</h4>
<pre class="syntaxbox"><strong>Operator:</strong> x /= y
<strong>Meaning:</strong> x = x / y
</pre>
<h4 id="示例_5">示例</h4>
<pre class="brush: js">// 假定已定义了下面的变量
// bar = 5
bar /= 2 // 2.5
bar /= "foo" // NaN
bar /= 0 // Infinity
</pre>
<h3 id="模赋值(Remainder_assignment)"><a name="Remainder_assignment">模赋值(Remainder assignment)</a></h3>
<p>模赋值运算符使一个变量除以右值,然后把余数赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "remainder operator", "#Remainder", 1)}}。</p>
<h4 id="语法_6">语法</h4>
<pre class="syntaxbox"><strong>Operator:</strong> x %= y
<strong>Meaning:</strong> x = x % y
</pre>
<h4 id="示例_6">示例</h4>
<pre class="brush: js">// Assuming the following variable
// bar = 5
bar %= 2 // 1
bar %= 'foo' // NaN
bar %= 0 // NaN
</pre>
<h3 id="指数赋值(Exponentiation_assignment)"><a id="Exponentiation_assignment" name="Exponentiation_assignment">指数赋值(Exponentiation assignment)</a></h3>
<p>指数赋值运算符使一个变量为底数、以右值为指数的指数运算(乘方)结果赋给该变量。更多细节查看 {{jsxref("Operators/Arithmetic_Operators", "算术运算符", "#Exponentiation", 1)}}。</p>
<h4 id="语法_7">语法</h4>
<pre class="syntaxbox"><strong>语法:</strong> x **= y
<strong>含义:</strong> x = x ** y
</pre>
<h4 id="示例_7">示例</h4>
<pre class="brush: js">// Assuming the following variable
// bar = 5
bar **= 2 // 25
bar **= 'foo' // NaN</pre>
<h3 id="左移赋值(Left_shift_assignment)"><a name="Left_shift_assignment">左移赋值(Left shift assignment)</a></h3>
<p>左移赋值运算符使变量向左移动指定位数的比特位,然后把结果赋给该变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "left shift operator", "#Left_shift", 1)}}。</p>
<h4 id="语法_8">语法</h4>
<pre class="syntaxbox"><strong>Operator:</strong> x <<= y
<strong>Meaning:</strong> x = x << y
</pre>
<h4 id="示例_8">示例</h4>
<pre class="brush: js">var bar = 5; // (00000000000000000000000000000101)
bar <<= 2; // 20 (00000000000000000000000000010100)
</pre>
<h3 id="右移赋值(Right_shift_assignment)"><a name="Right_shift_assignment">右移赋值(Right shift assignment)</a></h3>
<p>右移赋值运算符使变量向右移指定位数的比特位,然后把结果赋给该变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "right shift operator", "#Right_shift", 1)}}。</p>
<h4 id="语法_9">语法</h4>
<pre class="syntaxbox"><strong>Operator:</strong> x >>= y
<strong>Meaning:</strong> x = x >> y
</pre>
<h4 id="示例_9">示例</h4>
<pre class="brush: js">var bar = 5; // (00000000000000000000000000000101)
bar >>= 2; // 1 (00000000000000000000000000000001)
var bar = -5; // (-00000000000000000000000000000101)
bar >>= 2; // -2 (-00000000000000000000000000000010)
</pre>
<h3 id="无符号右移赋值(Unsigned_right_shift_assignment)"><a name="Unsigned_right_shift_assignment">无符号右移赋值(Unsigned right shift assignment)</a></h3>
<p>无符号右移赋值运算符向右移动指定数量的比特位,然后把结果赋给变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", " unsigned right shift operator", "#Unsigned_right_shift", 1)}}。</p>
<h4 id="语法_10">语法</h4>
<pre class="syntaxbox"><strong>Operator:</strong> x >>>= y
<strong>Meaning:</strong> x = x >>> y
</pre>
<h4 id="示例_10">示例</h4>
<pre class="brush: js">var bar = 5; // (00000000000000000000000000000101)
bar >>>= 2; // 1 (00000000000000000000000000000001)
var bar = -5; // (-00000000000000000000000000000101)
bar >>>= 2; // 1073741822 (00111111111111111111111111111110)</pre>
<h3 id="按位与赋值(Bitwise_AND_assignment)"><a name="Bitwise_AND_assignment">按位与赋值(Bitwise AND assignment)</a></h3>
<p>按位与赋值运算符使用两个操作值的二进制表示,执行按位与运算,并把结果赋给变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "bitwise AND operator", "#Bitwise_AND", 1)}}。</p>
<h4 id="语法_11">语法</h4>
<pre class="syntaxbox"><strong>Operator:</strong> x &= y
<strong>Meaning:</strong> x = x & y
</pre>
<h4 id="示例_11">示例</h4>
<pre class="brush: js">var bar = 5;
// 5: 00000000000000000000000000000101
// 2: 00000000000000000000000000000010
bar &= 2; // 0
</pre>
<h3 id="按位异或赋值(Bitwise_XOR_assignment)"><a name="Bitwise_XOR_assignment">按位异或赋值(Bitwise XOR assignment)</a></h3>
<p>按位异或赋值运算符使用两个操作值的二进制表示,执行二进制异或运算,并把结果赋给变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "bitwise XOR operator", "#Bitwise_XOR", 1)}}。</p>
<h4 id="语法_12">语法</h4>
<pre class="syntaxbox"><strong>Operator:</strong> x ^= y
<strong>Meaning:</strong> x = x ^ y
</pre>
<h4 id="示例_12">示例</h4>
<pre class="brush: js">var bar = 5;
bar ^= 2; // 7
// 5: 00000000000000000000000000000101
// 2: 00000000000000000000000000000010
// -----------------------------------
// 7: 00000000000000000000000000000111
</pre>
<h3 id="按位或赋值(Bitwise_OR_assignment)"><a name="Bitwise_OR_assignment">按位或赋值(Bitwise OR assignment)</a></h3>
<p>按位或赋值运算符使用两个操作值的二进制表示,执行按位或运算,并把结果赋给变量。更多细节查看 {{jsxref("Operators/Bitwise_Operators", "bitwise OR operator", "#Bitwise_OR", 1)}}。</p>
<h4 id="语法_13">语法</h4>
<pre class="syntaxbox"><strong>Operator:</strong> x |= y
<strong>Meaning:</strong> x = x | y
</pre>
<h4 id="示例_13">示例</h4>
<pre class="brush: js">var bar = 5;
bar |= 2; // 7
// 5: 00000000000000000000000000000101
// 2: 00000000000000000000000000000010
// -----------------------------------
// 7: 00000000000000000000000000000111
</pre>
<h2 id="示例_14">示例</h2>
<h3 id="带有赋值运算符的左值(Left_operand)">带有赋值运算符的左值(Left operand)</h3>
<p>在某些不常见的情况下,赋值运算符(如<code> x += y</code>)并不等同于表达式( <code>x = x + y</code>)。当一个赋值运算符的左值包含有一个赋值运算符时,左值只会被求值一次。例如:</p>
<pre class="brush: js">a[i++] += 5 // i 执行一次求值
a[i++] = a[i++] + 5 // i 执行两次求值
</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-assignment-operators', 'Assignment operators')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES2015', '#sec-assignment-operators', 'Assignment operators')}}</td>
<td>{{Spec2('ES2015')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-11.13', 'Assignment operators')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES1', '#sec-11.13', 'Assignment operators')}}</td>
<td>{{Spec2('ES1')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div class="hidden">该兼容表是由结构化数据生成。如果你愿意贡献数据,请克隆仓库 <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并提交 pull request.</div>
<p>{{Compat("javascript.operators.assignment")}}</p>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="/zh-CN/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators">算术运算符</a></li>
</ul>
|