Math.pow()
ES1+Returns the base raised to the exponent power, following IEEE 754 semantics: fractional, negative, and special-value exponents are all defined. Since ES2016 the ** operator computes the same result; Math.pow remains useful as a first-class function and in pre-ES2016 codebases. Note that a negative base with a non-integer exponent yields NaN.
Syntax
Math.pow(base, exponent)Parameters
base number The base number
exponent number The exponent used to raise the base
Return Value
A number representing the given base taken to the power of the given exponent
Examples
console.log(Math.pow(2, 3));
console.log(Math.pow(4, 0.5));
console.log(2 ** 3); // 대안 📌 When to Use
Use Math.pow() or the ** operator for genuine exponentiation: compound growth (interest, population, retry backoff delays of base * 2 ** attempt), geometric scaling (zoom levels, type scales where each step multiplies by a ratio), converting between logarithmic and linear domains (10 ** (db / 20) for audio gain), polynomial evaluation, and easing curves in animation (t ** 3 for cubic easing). Choose the operator form in modern code for readability, and the function form when you need to pass exponentiation around as a value, for example points.map(p => Math.pow(p, gamma)) reads fine, but Math.pow itself can be handed to composition utilities. Respect the IEEE edges: a negative base with a fractional exponent is NaN by specification (the principal root would be complex), so cube roots of negatives need Math.cbrt(); 0 ** 0 is defined as 1 in JavaScript; and 0 ** negative is Infinity. Integer results are exact only while they fit in a double, beyond 2^53 precision silently degrades, so cryptographic or big-integer exponentiation belongs to BigInt (2n ** 64n) rather than floats. For the special cases squaring and square roots, x * x and Math.sqrt(x) are clearer and at least as fast.
⚠️ Common Mistakes
Taking roots of negative numbers via fractional exponents: Math.pow(-8, 1/3) is NaN, not -2, because 1/3 is stored as a non-integer double and IEEE defines negative-base-fractional-exponent as NaN. Use Math.cbrt(-8), or handle sign explicitly: Math.sign(x) * Math.abs(x) ** (1/3).
Swapping the arguments: Math.pow(2, 10) is 1024 but Math.pow(10, 2) is 100, and both are plausible-looking numbers, so the transposition survives glance-level review. The ** operator (base ** exponent) makes the order visually unambiguous.
Writing -2 ** 2 and expecting 4 or -4 to come out consistently: JavaScript makes unparenthesized unary minus with ** a SyntaxError precisely because languages disagree; you must write (-2) ** 2 (which is 4) or -(2 ** 2) (which is -4).
Trusting float exactness for large powers: Math.pow(2, 53) + 1 equals Math.pow(2, 53), and Math.pow(10, 23) is actually 1.0000000000000001e+23 stored inexactly. Token counters, ID spaces, and financial powers beyond 2^53 need BigInt.
Accumulating error with repeated fractional powers: applying x ** 0.1 ten times does not return exactly x. Compound the exponent mathematically (x ** (n * 0.1)) instead of compounding the operation.
Using pow for decimal rounding scales: Math.pow(10, 2) as a scale factor is fine, but chains like value * 10 ** digits inherit the usual binary-decimal mismatch (4.35 * 100 = 434.999...); the rounding caveats from Math.round apply to the composed expression.
✅ Best Practices
Prefer ** in new code (2 ** 10, ratio ** steps): it reads as math, has well-defined precedence with parentheses enforced around unary minus, and compiles identically to Math.pow.
Encode growth formulas once with named parameters: compoundInterest(principal, rate, periods) wrapping principal * (1 + rate / n) ** (n * t), as in the example, so the finance-sensitive expression is tested in isolation rather than retyped per screen.
For roots, use the dedicated functions: Math.sqrt, Math.cbrt, and for other roots handle negative bases deliberately; a nthRoot(x, n) helper with sign logic documents the domain decision.
Keep exact-integer exponentiation in BigInt when results can exceed Number.MAX_SAFE_INTEGER; mixing BigInt and Number throws, which is a feature, it forces the precision decision to be explicit.
In hot paths, replace tiny integer powers with multiplication (x * x, x * x * x): it is exact, marginally faster, and clearer for the geometric formulas where squares and cubes dominate.
⚡ Performance Notes
Math.pow() and ** compile to the same runtime routine; neither is faster. Integer-exponent cases are heavily optimized (engines use exponentiation by squaring or direct multiplication for small constants), while fractional exponents go through a transcendental implementation (effectively exp(y·ln x)) costing tens of nanoseconds, several times an add or multiply but still cheap in absolute terms. For squaring and cubing in genuinely hot loops (physics, per-pixel effects), x * x is reliably as fast or faster and exact. Repeated powers of a constant base are better hoisted: precompute the scale table (const scales = steps.map(i => ratio ** i)) once rather than exponentiating per frame. As with all float math, denormal and special-value inputs (subnormals, Infinity, NaN) can hit slow paths; sanitize inputs at boundaries so inner loops see ordinary finite doubles.
🌍 Real World Example
Compound Interest Calculator
Compound interest is exponentiation with money attached: this example implements A = P(1 + r/n)^(nt), letting users compare how principal grows under different rates, compounding frequencies, and horizons. Isolating the formula in one function makes it unit-testable against known financial tables, which matters because a transposed exponent or misplaced division here misprices real decisions. The same math powers retirement projections, loan amortization previews, inflation adjustments, and the exponential-backoff delays (base * 2 ** attempt) that keep API clients polite under failure.
function calculateCompoundInterest(principal, rate, years, compoundsPerYear = 12) {
// A = P(1 + r/n)^(nt)
const amount = principal * Math.pow(
(1 + rate / compoundsPerYear),
compoundsPerYear * years
);
return {
finalAmount: amount.toFixed(2),
totalInterest: (amount - principal).toFixed(2),
growthMultiplier: (amount / principal).toFixed(3)
};
}
console.log(calculateCompoundInterest(10000, 0.07, 10));
// { finalAmount: '20096.61', totalInterest: '10096.61', growthMultiplier: '2.010' }