JavaScript Math.pow() and the ** Operator

Math.pow(base, exponent) raises a number to a power. Modern JavaScript also provides the exponentiation operator **, which is usually shorter and easier to read.

console.log(Math.pow(2, 5)); // 32
console.log(2 ** 5);           // 32

Math.pow() syntax Top ↑

Math.pow(base, exponent)

Basic powers Top ↑

console.log(Math.pow(5, 5)); // 3125
console.log(Math.pow(2, 2)); // 4
console.log(Math.pow(9, 0.5)); // 3

Negative exponents Top ↑

console.log(Math.pow(2, -2)); // 0.25
console.log(10 ** -3);          // 0.001

Negative bases Top ↑

console.log(Math.pow(-2, 2)); // 4
console.log(Math.pow(-2, 3)); // -8
console.log(Math.pow(-2, 0.5)); // NaN: no real square root

Zero and special cases Top ↑

console.log(Math.pow(0, 0)); // 1
console.log(Math.pow(0, 1)); // 0
console.log(Math.pow(1, 0)); // 1

Math.pow() vs the ** operator Top ↑

For Number values, the two forms are generally equivalent. The ** operator also works with BigInt when both operands are BigInts, while Math.pow() only works with numbers.

const square = 12 ** 2;
const cube = 12 ** 3;
console.log(square, cube); // 144 1728

Generating a small power table Top ↑

for (let exponent = 0; exponent < 5; exponent += 1) {
  const row = [];
  for (let base = 0; base < 5; base += 1) {
    row.push(Math.pow(base, exponent));
  }
  console.log(row.join(", "));
}

See also Math.exp().




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer