加算代入 (+=)
Baseline Widely 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)); // Addition
// Expected output: 5
console.log((b += " world")); // Concatenation
// Expected output: "hello world"
構文
js
x += y
解説
x += y
は x = x + y
と同等ですが、式 x
は一度しか評価されません。
例
加算代入の使用
js
let baz = true;
// 論理値 + 数値 -> 加算
baz += 1; // 2
// 論理値 + 論理値 -> 加算
baz += false; // 2
js
let foo = "foo";
// 文字列 + 論理値 -> 連結
foo += false; // "foofalse"
// 文字列 + 文字列 -> 連結
foo += "bar"; // "foofalsebar"
js
let bar = 5;
// 数値 + 数値 -> 加算
bar += 2; // 7
// 数値 + 文字列 -> 連結
bar += "foo"; // "7foo"
js
let x = 1n;
// 長整数 + 長整数 -> 加算
x += 2n; // 3n
// 長整数 + 数値 -> TypeError が発生
x += 1; // TypeError: Cannot mix BigInt and other types, use explicit conversions
仕様書
Specification |
---|
ECMAScript® 2025 Language Specification # sec-assignment-operators |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Addition assignment ( x += y ) |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.