UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Operators/ Addition assignment (+=)

The addition assignment (+=) operator performs addition (which is either numeric addition or string concatenation) on the two operands and assigns the result to the left operand.

Syntax

x += y

Description

x += y is equivalent to x = x + y, except that the expression x is only evaluated once.

Examples

Using addition assignment

let baz = true;

// Boolean + Number -> addition
baz += 1; // 2

// Number + Boolean -> addition
baz += false; // 2
let foo = "foo";

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

// String + String -> concatenation
foo += "bar"; // "foofalsebar"
let bar = 5;

// Number + Number -> addition
bar += 2; // 7

// Number + String -> concatenation
bar += "foo"; // "7foo"
let x = 1n;

// BigInt + BigInt -> addition
x += 2n; // 3n

// BigInt + Number -> throws TypeError
x += 1; // TypeError: Cannot mix BigInt and other types, use explicit conversions

Specifications

Browser compatibility

See also