冪賦值 (**=)

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

瀏覽器相容性

另見