此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Math.pow()

基线 广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

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)

参数

base

基数

exponent

指数

描述

由于 powMath 的静态方法,所以应该像这样使用:Math.pow(),而不是作为你创建的 Math 对象的方法。

示例

使用 Math.pow

js
function raisePower(x, y) {
  return Math.pow(x, y);
}

如果 x 是 2,且 y 是 7,则 raisePower 函数返回 128(2 的 7 次幂)。

规范

规范
ECMAScript® 2027 Language Specification
# sec-math.pow

浏览器兼容性

参见