Ship internal tools in hours, not weeks. Real auth, users, jobs, audit logs, and cohesive UI included. Early access $249 $499 → [Get it now]

How to raise a number to a power in JavaScript

Raising numbers to powers is essential for mathematical calculations, exponential growth models, compound interest, and geometric progressions in JavaScript applications. With over 25 years of experience in software development and as the creator of CoreUI, I’ve used exponentiation in calculators, financial tools, and chart scaling utilities. For square roots (power of 0.5), see how to get the square root of a number in JavaScript.

Use the ** exponentiation operator (ES2016) to raise a number to a power.

2 ** 3    // 8    — 2³
10 ** 2   // 100  — 10²
5 ** 0    // 1    — anything to the power of 0 is 1
2 ** -3   // 0.125 — negative exponent = 1/2³
9 ** 0.5  // 3    — fractional exponent = square root

1. Exponentiation Operator (**)

The ** operator is the modern, readable way to compute powers. It is right-associative.

2 ** 10    // 1024
3 ** 3     // 27
0.5 ** 2   // 0.25
(-2) ** 3  // -8   — parentheses required before unary minus
// -2 ** 2 → SyntaxError: unary minus cannot precede ** without parentheses

// Right-associativity: a ** b ** c = a ** (b ** c)
2 ** 3 ** 2   // 512  — same as 2 ** (3 ** 2) = 2 ** 9
(2 ** 3) ** 2 // 64   — left-to-right with explicit parentheses

Important: -2 ** 2 is a SyntaxError in JavaScript — always wrap the base in parentheses when it is a unary expression: (-2) ** 2.

2. Math.pow() — Legacy and Functional Contexts

Math.pow(base, exponent) predates ** and is still valid. Prefer it when you need a function reference (e.g. passing to Array.reduce).

Math.pow(2, 10)     // 1024
Math.pow(3, 3)      // 27
Math.pow(4, 0.5)    // 2    — square root
Math.pow(2, -1)     // 0.5
Math.pow(0, 0)      // 1    — matches ** behavior

// Useful as a higher-order function
[1, 2, 3, 4].map(n => Math.pow(n, 2))  // [1, 4, 9, 16]

3. Fractional and Negative Exponents

Fractional exponents compute roots; negative exponents compute reciprocals.

27 ** (1/3)   // 3      — cube root
16 ** 0.25    // 2      — fourth root
2 ** -1       // 0.5    — 1/2
2 ** -2       // 0.25   — 1/4
10 ** -3      // 0.001  — 1/1000

This is equivalent to Math.sqrt() for ** 0.5 but generalises to any root.

4. Compound Interest

A classic real-world use: A = P * (1 + r/n) ** (n * t).

function compoundInterest(principal, rate, timesPerYear, years) {
  return principal * ((1 + rate / timesPerYear) ** (timesPerYear * years))
}

compoundInterest(1000, 0.05, 12, 10)
// 1647.01 — $1000 at 5% annual interest compounded monthly for 10 years

5. Powers of 2 — Bit Shifting and Buffer Sizes

Powers of 2 appear frequently in buffer allocation, bitmasks, and data structure sizing.

2 ** 8    // 256   — max unsigned 8-bit value + 1
2 ** 16   // 65536
2 ** 32   // 4294967296

// Alternative using bit shifting (integers only, faster for small powers)
1 << 8    // 256
1 << 16   // 65536

Use ** for readability; use << only in performance-critical integer-only code.

6. Overflow and Precision

Very large exponents produce Infinity; very small negative exponents approach 0.

2 ** 1023   // 8.98846567431158e+307 — near max finite float
2 ** 1024   // Infinity — overflow
2 ** -1074  // 5e-324 — smallest positive float (denormalised)
2 ** -1075  // 0      — underflow

// BigInt exponentiation — exact for integers
2n ** 64n   // 18446744073709551616n — no overflow

For cryptographic or exact large-integer powers, use BigInt** works with BigInt operands natively.

Best Practice Note:

Use ** over Math.pow() in modern code — it is cleaner and part of the language standard since ES2016. Always parenthesise a unary minus before **: write (-x) ** n, not -x ** n (which is a SyntaxError). For square roots, Math.sqrt() is clearer than ** 0.5; use fractional exponents for other roots. Guard against Infinity when the exponent is large or the base exceeds the float range.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author