Math.pow()

Raises a base to the power of an exponent and returns the result.

Signature
Math.pow(base, exponent)
  • Added in JavaScript ES1

Parameters

ParameterTypeDescription
$basenumberThe base number.
$exponentnumberThe exponent the base is raised to.

Return value

numberA number representing base raised to the power of exponent. Returns NaN if exponent is NaN, if base is NaN and exponent is non-zero, if base is ±1 and exponent is ±Infinity, or if base is negative and exponent is not an integer.

Errors

Nothing for numeric input, but the ** operator throws a TypeError if one operand is a BigInt and the other is a Number.

Changelog

  • PHP ES2016The ** exponentiation operator was added as an equivalent shorthand; unlike Math.pow() it also works with BigInt operands (both operands must be BigInt).

Math.pow(base, exponent) computes base ** exponent. Since ES2016 the ** operator does the same thing with less noise, and it is the modern default for numeric code: 2 ** 10 rather than Math.pow(2, 10).

When to reach for it

Prefer Math.pow() when you need a function value — passing it to reduce, map, or a higher-order helper — or when supporting an environment that predates **. Use ** everywhere else. Note ** is right-associative (2 ** 3 ** 2 is 512, not 64) and refuses an unparenthesised unary minus on its left: -2 ** 2 is a SyntaxError, while Math.pow(-2, 2) is fine.

What trips people up

A negative base with a fractional exponent is NaN. Math.pow(-8, 1/3) is NaN, not -2, because the result is not a real number in general and 1/3 is not exactly one third in binary. For cube roots of negatives use Math.cbrt().

The other oddity is Math.pow(1, Infinity), which is NaN rather than the IEEE 754 answer of 1 — a deliberate legacy deviation shared by **. Math.pow(NaN, 0), meanwhile, is 1: the only case where NaN does not propagate.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Computing compound interest or exponential growth over N periods
  • Converting between byte units with powers of 1024
  • Squaring or cubing a value inside a map or reduce callback
  • Taking an nth root as Math.pow(x, 1 / n) for a non-negative x

Frequently asked questions

Should I use Math.pow() or the ** operator?

Use ** for ordinary arithmetic — it is shorter and supports BigInt. Reach for Math.pow() when you need to pass the operation as a function value or when targeting engines older than ES2016.

Why does Math.pow(-8, 1/3) return NaN?

Because a negative base with a non-integer exponent has no real result in the general case, so the spec returns NaN. Use Math.cbrt(-8) to get -2.

How do I calculate an nth root in JavaScript?

Use Math.pow(x, 1 / n) for a non-negative x, or Math.sqrt and Math.cbrt for the second and third roots. Negative values only work with Math.cbrt, since fractional exponents on a negative base give NaN.

More examples

Operator differences worth knowing

** chains right to left, so 2 ** 3 ** 2 is 2 ** 9. It also rejects an unparenthesised unary minus on the left to avoid ambiguity, which Math.pow never has to worry about. Only ** accepts BigInt operands, and both sides must then be BigInt.

Example · javascript
console.log(2 ** 3 ** 2);        // 512 — right-associative
console.log((2 ** 3) ** 2);      // 64
console.log(Math.pow(Math.pow(2, 3), 2)); // 64

console.log((-2) ** 2);          // 4; bare -2 ** 2 is a SyntaxError
console.log(Math.pow(-2, 2));    // 4, no parentheses needed

console.log(2n ** 10n);          // 1024n — BigInt works with **
console.log(Math.pow(2, 10));    // 1024

Roots and unit scaling

Powers of 1024 drive the unit conversion. The last two lines show fractional exponents used as nth roots, which is exact enough for these values but only valid when the base is non-negative — a negative base with a fractional exponent gives NaN.

Example · javascript
function formatBytes(n) {
  const i = Math.floor(Math.log2(n) / 10);
  const unit = ["B", "KiB", "MiB", "GiB"][i];
  return (n / Math.pow(1024, i)).toFixed(1) + " " + unit;
}

console.log(formatBytes(2048));      // "2.0 KiB"
console.log(formatBytes(5_242_880)); // "5.0 MiB"

// nth root via a fractional exponent — non-negative bases only
console.log(Math.pow(81, 1 / 4));    // 3
console.log(Math.pow(32, 1 / 5));    // 2

Discussion

  • Be the first to comment on this lesson.