JavaScript pow() 方法

定义和用法

pow() 方法可返回 x 的 y 次幂的值。

语法

  1. Math.pow(x,y)
参数 描述
x 必需。底数。必须是数字。
y 必需。幂数。必须是数字。

返回值

x 的 y 次幂。

说明

如果结果是虚数或负数,则该方法将返回 NaN。如果由于指数过大而引起浮点溢出,则该方法将返回 Infinity。

实例

在下面的例子中,我们将把 pow() 运用到不同的数字组合上:

  1. <script type="text/javascript">
  2.  
  3. document.write(Math.pow(0,0) + "<br />")
  4. document.write(Math.pow(0,1) + "<br />")
  5. document.write(Math.pow(1,1) + "<br />")
  6. document.write(Math.pow(1,10) + "<br />")
  7. document.write(Math.pow(2,3) + "<br />")
  8. document.write(Math.pow(-2,3) + "<br />")
  9. document.write(Math.pow(2,4) + "<br />")
  10. document.write(Math.pow(-2,4) + "<br />")
  11.  
  12. </script>

输出:

  1. 1
  2. 0
  3. 1
  4. 1
  5. 8
  6. -8
  7. 16
  8. 16