JavaScript中计算N次方的方法**和math.pow()-讲解

Posted JackieDYH

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript中计算N次方的方法**和math.pow()-讲解相关的知识,希望对你有一定的参考价值。

指数运算符(**)

console.log(2 ** 2); // 4
console.log(3 ** 2); // 9
console.log('3' ** '2'); // 9
console.log((() => {
    let x = 2;
    let y = 3;
    x **= y;
    return x;
})()); // 8

Math.pow()

返回基础的指数次幂

Math.pow(x, y)  //x:基数    y:指数

console.log(Math.pow(2, 2)); // 4
console.log(Math.pow(3, 2)); // 9
console.log(Math.pow('3', '2')); // 9
let x = 2;
x = Math.pow(x, 3);
console.log(x); // 8

 

以上是关于JavaScript中计算N次方的方法**和math.pow()-讲解的主要内容,如果未能解决你的问题,请参考以下文章