此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in English Always switch to English

指數運算子(**)

Baseline
Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2017年3月.

指數運算子(**)會回傳以第一個數字作為底數;第二個數字做為指數的運算結果。

它類同於 Math.pow,不一樣的是 ** 可以用於 BigInt 的計算而 Math.pow 不行。

嘗試一下

console.log(3 ** 4);
// Expected output: 81

console.log(10 ** -2);
// Expected output: 0.01

console.log(2 ** (3 ** 2));
// Expected output: 512

console.log((2 ** 3) ** 2);
// Expected output: 64

語法

js
x ** y

簡介

指數運算子是 right-associative: