js脚本中的pow()方法与求幂运算符(**)都可以返回一个数的n次方法,也就是求幂运算。详细的使用方法,可参考下面博文内容。
js求幂运算的方法
方法1:js 使用pow()进行求幂运算
pow():可以方法返回 x 的 y 次幂。
语法:
Math.pow(x,y);
参数:
x:必需。底数。必须是数字。
y:必需。幂数。必须是数字。
示例:
console.log(Math.pow(1,1)); // 1 console.log(Math.pow(0,1)); // 0 console.log(Math.pow(2,2)); // 4 console.log(Math.pow(2,6)); // 64 console.log(Math.pow(2,13000)); // Infinity
PS:
1、pow() 方法结果如果是虚数或负数,则该方法将返回 NaN。
2、pow() 方法结果如果由于指数过大而引起浮点溢出,则该方法将返回 Infinity。
方法2:js 使用求幂运算符(**)来进行求幂运算
** : 为JS中的求幂运算符,左边的值为底数、以右边的值为指数的指数运算,也就是求幂运算
语法:
a = x ** y;
参数:
x:必需。底数。必须是数字。
y:必需。幂数。必须是数字。
例:
console.log(1 ** 2); // 1 console.log(0 ** 1); // 0 console.log(2 ** 2); // 4 console.log(2 ** 6); // 64 console.log(2 ** 13000); // Infinity