Math.pow(base, exponent) // 返回 base 的 exponent 次冪
base:底數 (Number)exponent:指數 (Number)base^exponent (Number),特殊情況返回 NaN/Infinity。console.log(Math.pow(7, 2)); // 49
console.log(Math.pow(8, 8)); // 16777216
console.log(Math.pow(-1, 2)); // 1
console.log(Math.pow(0, 10)); // 0
console.log(Math.pow(2, 0.5)); // 1.414... (√2)
console.log(Math.pow(2, -1)); // 0.5
console.log(Math.pow(-2, 0.5)); // NaN (負數非整數次方)
**2 ** 3 // 8
2 ** 0.5 // 1.414...
(-2) ** 2 // 4 (注意括號:-2**2 === -(2**2) === -4)
NaN ** 0 // 1 (規範定義)
** 為右結合,優先級高於一元運算符,語法更簡潔、可鏈式 2 ** 3 ** 2 === 2 ** 9 === 512。
principal * Math.pow(1 + rate, years)Math.pow(2, n) === 1 << n (n 為非負整數)| 輸入 | 結果 | 說明 |
|---|---|---|
| Math.pow(NaN, 0) | 1 | IEEE 754 規範 |
| Math.pow(1, Infinity) | 1 | 極限定義 |
| Math.pow(0, -1) | Infinity | 除零 |
| Math.pow(-Infinity, 0.5) | NaN | 負數非整數次方 |