Math.pow()
Math.pow(base, exponent) Math.pow() returns the base raised to the exponent power — the mathematical exponentiation function. It’s a static method of the Math object, meaning you always call it as Math.pow(base, exponent), not on an instance.
Syntax
Math.pow(base, exponent)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
base | number | — | The base number |
exponent | number | — | The exponent to raise the base to |
Return Value
The result of raising base to the power of exponent.
Examples
Basic integer powers
Math.pow(2, 3);
// 8 (2³ = 2 × 2 × 2)
Math.pow(5, 2);
// 25 (5² = 5 × 5)
Math.pow(10, 4);
// 10000 (10⁴)
Comparing ** and Math.pow()
ES2016 introduced the ** exponentiation operator as a more readable alternative to Math.pow(). The operator reads more naturally in mathematical expressions and chains without extra nesting. For simple integer powers, both produce the same result, though their syntax makes different things convenient:
Math.pow(2, 3) === 2 ** 3; // true (both are 8)
Math.pow(4, 0.5); // 2 (square root)
4 ** 0.5; // 2 (same result)
// The main difference: ** allows chaining without parentheses
(2 ** 3) ** 2; // 64 (2³ = 8, then 8² = 64)
Math.pow(Math.pow(2, 3), 2); // 64 (more verbose)
Prefer ** for readability in modern code, but Math.pow() remains useful for explicit intent or when passing as a callback. One place where the difference becomes visible is edge-case behavior: zero, negative, and fractional exponents produce results that are worth knowing before you write the code.
Special values and edge cases
// Zero and negative exponents
Math.pow(5, 0); // 1 (any number to the power of 0 is 1)
Math.pow(5, -2); // 0.04 (5⁻² = 1/25)
// Infinity
Math.pow(2, Infinity); // Infinity
Math.pow(Infinity, 2); // Infinity
Math.pow(0, Infinity); // NaN
// NaN
Math.pow(NaN, 2); // NaN
Math.pow(2, NaN); // NaN
// Negative base with fractional exponent
Math.pow(-4, 0.5); // NaN (no real square root of negative)
Math.pow(-8, 1/3); // -2 (cube root of negative is valid)
// Negative base with integer exponent
Math.pow(-2, 3); // -8 (-2 × -2 × -2)
Math.pow(-2, 4); // 16
The critical edge case: negative base with a fractional exponent returns NaN because there’s no real result. However, fractional exponents that result in an odd-denominator root (like 1/3 for cube root) do work correctly. A few more special combinations behave in specific, sometimes surprising ways — the summary below covers the ones you are most likely to hit.
Edge cases summary
Math.pow(0, 0); // 1 (by convention)
Math.pow(0, positive); // 0
Math.pow(0, negative); // Infinity (1/0)
Math.pow(1, Infinity); // NaN
Math.pow(-1, Infinity); // NaN
Math.pow(1, any); // 1
Math.pow() vs the ** operator
ES2016 introduced the ** exponentiation operator as syntactic sugar for Math.pow(). The two produce identical results in nearly all cases, with one nuance: Math.pow(-1, NaN) returns NaN, while in some edge cases with large negative bases the spec produces the same result from both. For day-to-day code, they are interchangeable:
2 ** 10; // 1024
Math.pow(2, 10); // 1024
// ** allows cleaner chaining
2 ** 3 ** 2; // 512 (right-associative: 2 ** (3 ** 2) = 2 ** 9)
Math.pow(2, Math.pow(3, 2)); // 512 (same, more verbose)
Prefer ** in modern code for brevity. Keep Math.pow() when passing the function as a value (e.g., .reduce(Math.pow)) or when supporting environments without ** support.
Square roots and fractional exponents
You can compute roots using fractional exponents. A square root is the 1/2 power, a cube root is 1/3, and so on:
Math.pow(9, 0.5); // 3 (square root of 9)
Math.pow(27, 1/3); // 3 (cube root of 27)
Math.pow(16, 0.25); // 2 (fourth root of 16)
// Math.sqrt is more readable for square roots
Math.sqrt(9); // 3 (equivalent, clearer intent)
Negative bases with non-integer exponents return NaN because the result would be complex. Math.pow(-8, 1/3) is an exception — JavaScript returns -2 for this specific case because the fractional exponent simplifies to an integer ratio, but do not rely on this for general cube roots of negatives.
Performance
Math.pow() and ** are similarly fast on modern engines. Use whichever reads better in context.
When Math.pow() still helps
Math.pow() still matters when you are reading older code, teaching older syntax, or targeting environments where the exponentiation operator is not available. It also keeps the operation explicit in code that wants the function-call style for consistency with other Math helpers.
Even in modern code, Math.pow() can be easier to scan inside formulas that already use several Math functions. The important thing is to use one style consistently within a file so the arithmetic stays easy to follow.
See Also
- Math.sqrt() — Square root
- Math.exp() — e to the power of a number
- Math.log() — Natural logarithm