除法赋值 (/=)

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

浏览器兼容性

另见