除法賦值 (/=)

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

除法賦值運算子 (/=) 對兩個運算元執行除法,並將結果賦給左運算元。

試一試

let a = 3;

a /= 2;
console.log(a);
// Expected output: 1.5

a /= 0;
console.log(a);
// Expected output: Infinity

a /= "hello";
console.log(a);
// Expected output: NaN

語法

js
x /= y

描述

x /= y 等價於 x = x / y,不同之處在於表示式 x 只會被評估一次。

示例

使用數字進行除法賦值

js
let bar = 5;

bar /= 2; // 2.5
bar /= 2; // 1.25
bar /= 0; // Infinity

其他非 BigInt 值被強制轉換為數字

js
let bar = 5;
bar /= "2"; // 2.5
bar /= "foo"; // NaN

使用 BigInts 進行除法賦值

js
let foo = 3n;
foo /= 2n; // 1n
foo /= 2n; // 0n

foo /= 0n; // RangeError: BigInt division by zero
foo /= 1; // TypeError: Cannot mix BigInt and other types, use explicit conversions

規範

規範
ECMAScript® 2026 語言規範
# sec-assignment-operators

瀏覽器相容性

另見