Math.pow()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Math.pow()
函数返回基数(base
)的指数(exponent
)次幂,即 base^exponent
。
尝试一下
console.log(Math.pow(7, 3)); // Expected output: 343 console.log(Math.pow(4, 0.5)); // Expected output: 2 console.log(Math.pow(7, -2)); // Expected output: 0.02040816326530612 // (1/49) console.log(Math.pow(-7, 0.5)); // Expected output: NaN
语法
Math.pow(base, exponent)
参数
描述
由于 pow
是 Math
的静态方法,所以应该像这样使用:Math.pow()
,而不是作为你创建的 Math
对象的方法。
示例
使用 Math.pow
js
function raisePower(x, y) { return Math.pow(x, y); }
如果 x
是 2,且 y
是 7,则 raisePower 函数返回 128(2 的 7 次幂)。
规范
Specification |
---|
ECMAScript® 2026 Language Specification # sec-math.pow |