試一試
let a = 2;
console.log((a -= 3));
// Expected output: -1
console.log((a -= "Hello"));
// Expected output: NaN
語法
js
x -= y
描述
x -= y 等效於 x = x - y,不同之處在於表示式 x 只被計算一次。
示例
使用數字進行減法賦值
js
let bar = 5;
bar -= 2; // 3
其他非 BigInt 值被強制轉換為數字
js
bar -= "foo"; // NaN
使用 BigInt 進行減法賦值
js
let foo = 3n;
foo -= 2n; // 1n
foo -= 1; // TypeError: Cannot mix BigInt and other types, use explicit conversions
規範
| 規範 |
|---|
| ECMAScript® 2026 語言規範 # sec-assignment-operators |
瀏覽器相容性
載入中…