幂赋值 (**=)

Baseline 已广泛支持

此特性已得到良好确立,可跨多种设备和浏览器版本使用。自 2017 年 3 月起,所有浏览器均支持此特性。

幂赋值运算符(**=对两个操作数执行幂运算,并将结果赋给左操作数。

试一试

let a = 3;

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

console.log((a **= 0));
// 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; // 25

其他非 BigInt 值被强制转换为数字

js
let baz = 5;
baz **= "foo"; // NaN

使用 BigInt 的幂赋值

js
let foo = 3n;
foo **= 2n; // 9n
foo **= 1; // TypeError: Cannot mix BigInt and other types, use explicit conversions

规范

规范
ECMAScript® 2026 语言规范
# sec-assignment-operators

浏览器兼容性

另见