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