加算代入演算子 (+=)
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
加算代入 (+=
) 演算子は、 2 つのオペランドの加算(数値の加算または文字列の結合のどちらか)を実行し、左オペランドへ結果を代入します。
試してみましょう
let a = 2;let b = "hello";console.log((a += 3)); // 加算// 予想される出力: 5console.log((b += " world")); // 結合// 予想される出力: "hello world"
構文
js
x += y
解説
x += y
はx = x + y
と同等ですが、式x
は一度しか評価されません。
例
数値を使用した加算代入
js
let bar = 5;bar += 2; // 7
それ以外の文字列でも長整数でもない値は、数値に変換されます。
js
let baz = true;baz += 1; // 2baz += false; // 2
長整数を使用した加算代入
js
let x = 1n;x += 2n; // 3nx += 1; // TypeError: Cannot mix BigInt and other types, use explicit conversions
文字列を使用した加算代入
js
let foo = "foo";foo += false; // "foofalse"foo += "bar"; // "foofalsebar"let bar = 5;bar += "foo"; // "5foo"
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification # sec-assignment-operators |