试一试
let a = 2;
console.log((a *= 3));
// Expected output: 6
console.log((a *= "hello"));
// Expected output: NaN
语法
js
x *= y
描述
x *= y 等价于 x = x * y,不同之处在于表达式 x 只会被评估一次。
示例
使用数字的乘法赋值
js
let bar = 5;
bar *= 2; // 10
其他非 BigInt 值被强制转换为数字
js
let bar = 5;
bar *= "foo"; // NaN
使用 BigInt 的乘法赋值
js
let foo = 3n;
foo *= 2n; // 6n
foo *= 1; // TypeError: Cannot mix BigInt and other types, use explicit conversions
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-assignment-operators |
浏览器兼容性
加载中…