幂赋值(**=)对两个操作数执行幂运算,并将结果赋给左操作数。
JavaScript Demo: Expressions - Exponentiation assignment operator
let a = 3;
console.log((a **= 2));
// Expected output: 9
console.log((a **= 0));
// Expected output: 1
console.log((a **= "hello"));
// Expected output: NaN
语法
x **= y
描述
x **= y 和 x = x ** y 是等价的,不同之处在于前者的 x 只被计算一次。
示例
使用数字进行幂赋值
let bar = 5;
bar **= 2; // 25
其他非 BigInt 值会被强制转换为数字:
let baz = 5;
baz **= "foo"; // NaN
使用 BigInt 进行幂赋值
let foo = 3n;
foo **= 2n; // 9n
foo **= 1; // TypeError: Cannot mix BigInt and other types, use explicit conversions
规范
浏览器兼容性
参见
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
