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.
Try it
Syntax
js
x += y
Description
x += y
is equivalent to x = x + y
.
Examples
Using addition assignment
js
let baz = true;
// Boolean + Number -> addition
baz += 1; // 2
// Number + Boolean -> addition
baz += false; // 2
js
let foo = "foo";
// String + Boolean -> concatenation
foo += false; // "foofalse"
// String + String -> concatenation
foo += "bar"; // "foofalsebar"
js
let bar = 5;
// Number + Number -> addition
bar += 2; // 7
// Number + String -> concatenation
bar += "foo"; // "7foo"
js
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
Specification |
---|
ECMAScript Language Specification # sec-assignment-operators |
Browser compatibility
BCD tables only load in the browser