JavaScript表示x的y次幂


一、指数运算符(**)

示例

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()

1.定义

Math.pow()方法返回基础的指数次幂。

2.语法

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

3.示例

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表示x的y次幂


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM