Skip to content

Exponentiation (**): powers without Math.pow (ES2016)

  • How ** and **= relate to Math.pow(), and why -2 ** 2 is a syntax error.
  • The edge cases: fractional exponents, NaN, Infinity, strings and BigInt.
  • How to use it safely for exponential retry backoff.

TL;DR: base ** exponent returns base raised to the power exponent, and x **= y is shorthand for x = x ** y. For numbers it gives the same result as Math.pow(base, exponent). It also accepts two BigInts. It binds tighter than * and /, and it is right-associative.

It was added in ECMAScript 2016, the first yearly edition, and is supported by every current browser and server runtime (details in Compatibility).

Before ES2016, every power went through a method call. Nested powers read inside-out, and a formula with several of them was hard to compare with its written math. Python, Ruby and PHP already had an infix power operator.

Running the samples: save each complete sample as a .mjs file and run it with Node.js 22 (node sample.mjs). All outputs shown were produced with Node.js 22.23.

// Before ES2016: Math.pow for every power
const retryDelayOld = 250 * Math.pow(2, 3);
const volumeOld = Math.pow(Math.pow(2, 3), 2);
// ES2016: the ** operator and its compound form **=
const retryDelay = 250 * 2 ** 3;
const volume = (2 ** 3) ** 2;
let scale = 10;
scale **= 3;
console.log(retryDelayOld, retryDelay); // 2000 2000
console.log(volumeOld, volume); // 64 64
console.log(scale); // 1000
// Right-associative: a ** b ** c is a ** (b ** c)
console.log(2 ** 3 ** 2, (2 ** 3) ** 2); // 512 64
// Higher precedence than * and /, so no parentheses needed here
console.log(3 * 2 ** 2); // 12
// A unary operator directly before the base is a SyntaxError; parenthesize instead
console.log((-2) ** 2, -(2 ** 2)); // 4 -4
try {
new Function('return -2 ** 2');
} catch (e) {
console.log(e.name + ': ' + e.message);
}
// Same results as Math.pow for numbers, including the odd cases
console.log(2 ** -1, 2 ** 0.5, 4 ** 0.5, (-8) ** (1 / 3));
console.log(NaN ** 0, 1 ** Infinity, 0 ** -1, 2 ** 1024);
console.log(2 ** '3', 2 ** 'x');
// BigInt operands (ES2020): both sides must be BigInt, exponent must be >= 0
console.log(2n ** 64n);
for (const expr of ['2n ** 2', '2n ** -1n', 'Math.pow(2n, 2n)']) {
try {
console.log(new Function(`return ${expr}`)());
} catch (e) {
console.log(expr.padEnd(17), e.name);
}
}

Output:

512 64
12
4 -4
SyntaxError: Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence
0.5 1.4142135623730951 2 NaN
1 NaN Infinity Infinity
8 NaN
18446744073709551616n
2n ** 2 TypeError
2n ** -1n RangeError
Math.pow(2n, 2n) TypeError

What the output shows:

  • Associativity. ** groups from the right, as in mathematical notation: 2 ** 3 ** 2 is 2 ** 9. Every other binary arithmetic operator groups from the left.
  • Precedence. ** binds tighter than *, / and %, so 3 * 2 ** 2 is 12.
  • No ambiguous unary operand. Languages disagree about -2 ** 2 (Python gives -4, Bash gives 4), so the ECMAScript grammar does not allow a unary operator (-, +, !, ~, typeof, void, delete, await) directly before the base. You must write (-2) ** 2 or -(2 ** 2). The error message above is V8’s wording. The exponent side is not restricted: 2 ** -1 is fine.
  • Numbers. ** and Math.pow use the same algorithm in the spec (Number::exponentiate), so they return the same values. Fractional powers of negative bases are NaN, because the real root is not computed. NaN ** 0 is 1, while 1 ** Infinity is NaN, kept for backward compatibility even though IEEE 754 says 1. Results beyond the double range become Infinity.
  • Coercion. Non-BigInt operands are converted with ToNumeric, so 2 ** '3' is 8 and 2 ** 'x' is NaN.
  • BigInt. If both operands are BigInts, the result is an exact BigInt. Mixing a BigInt with a number throws a TypeError, and a negative BigInt exponent throws a RangeError. Math.pow never accepts BigInts, so ** is the only built-in power for them (see BigInt).

OrdersPortal retries failed calls to its payment API at api.example.test with exponential backoff: the wait doubles after each failure, up to a cap, and “full jitter” picks a random delay below that ceiling so many clients don’t retry in lockstep.

// backoff.mjs - OrdersPortal: retry delays for the payment API at api.example.test.
// delay = min(cap, base * factor ** attempt), then "full jitter" picks a random delay below it.
export function backoffDelay(attempt, { baseMs = 200, factor = 2, capMs = 10_000, random = Math.random } = {}) {
const ceiling = Math.min(capMs, baseMs * factor ** attempt);
return Math.round(random() * ceiling);
}
// Deterministic "random" so the output is reproducible: always the upper bound.
const noJitter = () => 1;
console.log(Array.from({ length: 8 }, (_, attempt) => backoffDelay(attempt, { random: noJitter })));
// Without the cap, the ceiling grows past any reasonable wait, and eventually to Infinity.
console.log(200 * 2 ** 20, 200 * 2 ** 1100);
// Compound interest on an overdue OrdersPortal invoice: 1.5% per month for 6 months, in cents.
const principalCents = 120_000;
const owedCents = Math.round(principalCents * (1 + 0.015) ** 6);
console.log(owedCents); // 131213
// Parsing data sizes from config: "4 KiB" -> 4 * 2 ** 10
const units = { B: 0, KiB: 10, MiB: 20, GiB: 30 };
const toBytes = (text) => {
const [, n, unit] = /^(\d+)\s*(B|KiB|MiB|GiB)$/.exec(text);
return Number(n) * 2 ** units[unit];
};
console.log(toBytes('4 KiB'), toBytes('512 MiB'), toBytes('2 GiB'));

Output:

[
200, 400, 800,
1600, 3200, 6400,
10000, 10000
]
209715200 Infinity
131213
4096 536870912 2147483648

The formulas read like their written form: base * factor ** attempt needs no parentheses because ** binds first. The cap is not optional: without it, attempt 20 waits 58 hours, and a large enough attempt number gives Infinity. The interest calculation rounds once, at the end, on a value in cents.

  • Writing -x ** 2. It is a SyntaxError, not -x². Write -(x ** 2) or (-x) ** 2, whichever you mean.
  • Using ^ for powers. ^ is bitwise XOR in JavaScript: 2 ^ 3 is 1.
  • Expecting left-to-right grouping. a ** b ** c is a ** (b ** c). Add parentheses when you mean (a ** b) ** c.
  • Taking roots of negative numbers. (-8) ** (1 / 3) is NaN; use Math.cbrt(-8) for cube roots.
  • Mixing BigInt and Number. 2n ** 2 throws. Convert one side: 2n ** BigInt(2) or Number(2n) ** 2.
  • Assuming exact results for large powers of Number. 3 ** 40 exceeds Number.MAX_SAFE_INTEGER, so the result is rounded. Use 3n ** 40n when you need every digit.
Approach Accepts BigInt Can be passed as a function Notes
a ** b Yes (both BigInt) No Default choice; reads like math
a **= b Yes No In-place update
Math.pow(a, b) No Yes, e.g. values.reduce(Math.pow) Same result for numbers
Math.sqrt(x), Math.cbrt(x) No Yes Clearer than x ** 0.5; cbrt handles negative input
1 << n No No Powers of two up to 2^30 as 32-bit integers; 1 << 31 is negative
Math.exp(x) No Yes Powers of e

Use ** in new code. Keep Math.pow where you need a function value, and use Math.sqrt/Math.cbrt when the intent is a root.

According to MDN’s browser compatibility data, ** and **= are supported in Chrome 52, Edge 14, Firefox 52, Safari 10.1 (Safari on iOS 10.3), Node.js 7.0.0, Deno 1.0 and Bun 1.0. Internet Explorer never supported them. BigInt operands need BigInt support (ES2020).

TypeScript and transpilers: with a target below ES2016, TypeScript rewrites a ** b as Math.pow(a, b) and a **= b as a = Math.pow(a, b). For BigInt operands it reports TS2791: Exponentiation cannot be performed on 'bigint' values unless the 'target' option is set to 'es2016' or later. (checked with TypeScript 6.0.3), because the rewrite would call Math.pow with BigInts.

1. What do 2 ** 3 ** 2 and (-2) ** 2 return, and why is -2 ** 2 an error? 512, because ** is right-associative, and 4. -2 ** 2 is a SyntaxError because the grammar forbids a unary operator directly before the base: other languages disagree on whether it means -4 or 4, so JavaScript makes you parenthesize.

2. Is ** just syntax sugar for Math.pow? For numbers, yes: both use the same spec algorithm and return identical values. But ** also works with BigInts, and Math.pow is a function you can pass around or replace, while an operator cannot be.

3. Senior scenario: a client SDK retries failed requests with delay = 100 * 2 ** attempt. During an outage your API is flooded in waves and some clients stop retrying for days. What do you change? Cap the delay (Math.min(capMs, ...)) so late attempts stay bounded, and cap the number of attempts. Add jitter, for example a random value between 0 and the capped ceiling, so clients that failed together don’t retry together. Honor a server Retry-After header when present, and make the base, cap and attempt limit configurable so they can be tuned without a release.

  • ** (ES2016) raises a number or BigInt to a power; **= updates in place.
  • It is right-associative, binds tighter than *, and rejects a unary operator before the base.
  • For numbers it matches Math.pow exactly; for BigInts both operands must be BigInt.