--- title: 加法赋值 (+=) slug: Web/JavaScript/Reference/Operators/Addition_assignment tags: - += translation_of: Web/JavaScript/Reference/Operators/Addition_assignment ---
{{jsSidebar("Operators")}}

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

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

Syntax

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

Examples

Using addition assignment

// 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"

Specifications

Specification
{{SpecName('ESDraft', '#sec-assignment-operators', 'Assignment operators')}}

Browser compatibility

{{Compat("javascript.operators.addition_assignment")}}

See also