このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docsコミュニティーについてもっと知り、仲間になるにはこちらから。
減算代入演算子 (-=)
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
減算代入演算子 (*=) は、2 つのオペランドで減算を行い、結果を左オペランドに代入します。
In this article
試してみましょう
let a = 2;console.log((a -= 3));// 予想される結果: -1console.log((a -= "Hello"));// 予想される結果: NaN構文
js
x -= y解説
x -= y はx = x - y と同等ですが、式x が一度だけ評価される点が異なります。
例
>数値を使用した減算代入
js
let bar = 5;bar -= 2; // 3その他の長整数以外の値は数値に変換されます。
js
bar -= "foo"; // NaN長整数を使用した減算代入
js
let foo = 3n;foo -= 2n; // 1nfoo -= 1; // TypeError: Cannot mix BigInt and other types, use explicit conversions仕様書
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-assignment-operators> |