aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/operators/addition_assignment/index.html
blob: 9469f0f0b77e1350cc6b955ebfb20a9a77f09e1b (plain)
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
---
title: 加法赋值 (+=)
slug: Web/JavaScript/Reference/Operators/Addition_assignment
tags:
  - +=
translation_of: Web/JavaScript/Reference/Operators/Addition_assignment
---
<div>{{jsSidebar("Operators")}}</div>

<p>加法赋值操作符 (<code>+=</code>) 将右操作数的值添加到变量,并将结果分配给该变量。两个操作数的类型确定加法赋值运算符的行为。加法或串联是可能的。</p>

<div>{{EmbedInteractiveExample("pages/js/expressions-addition-assignment.html")}}</div>

<div></div>



<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox notranslate"><strong>Operator:</strong> x += y
<strong>Meaning:</strong>  x  = x + y</pre>

<h2 id="Examples">Examples</h2>

<h3 id="Using_addition_assignment">Using addition assignment</h3>

<pre class="brush: js notranslate">// Assuming the following variables
//  foo = 'foo'
//  bar = 5
//  baz = true

// Number + Number -&gt; addition
bar += 2 // 7

// Boolean + Number -&gt; addition
baz += 1 // 2

// Boolean + Boolean -&gt; addition
baz += false // 1

// Number + String -&gt; concatenation
bar += 'foo' // "5foo"

// String + Boolean -&gt; concatenation
foo += false // "foofalse"

// String + String -&gt; concatenation
foo += 'bar' // "foobar"</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-assignment-operators', 'Assignment operators')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>



<p>{{Compat("javascript.operators.addition_assignment")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Assignment">Assignment operators in the JS guide</a></li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Addition">Addition operator</a></li>
</ul>