Exponentiation (**): powers without Math.pow (ES2016)
What you’ll learn
Section titled “What you’ll learn”- How
**and**=relate toMath.pow(), and why-2 ** 2is a syntax error. - The edge cases: fractional exponents,
NaN,Infinity, strings and BigInt. - How to use it safely for exponential retry backoff.
Summary
Section titled “Summary”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).
The problem it solves
Section titled “The problem it solves”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 powerconst 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 2000console.log(volumeOld, volume); // 64 64console.log(scale); // 1000How it works
Section titled “How it works”// 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 hereconsole.log(3 * 2 ** 2); // 12
// A unary operator directly before the base is a SyntaxError; parenthesize insteadconsole.log((-2) ** 2, -(2 ** 2)); // 4 -4try { new Function('return -2 ** 2');} catch (e) { console.log(e.name + ': ' + e.message);}
// Same results as Math.pow for numbers, including the odd casesconsole.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 >= 0console.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 64124 -4SyntaxError: Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence0.5 1.4142135623730951 2 NaN1 NaN Infinity Infinity8 NaN18446744073709551616n2n ** 2 TypeError2n ** -1n RangeErrorMath.pow(2n, 2n) TypeErrorWhat the output shows:
- Associativity.
**groups from the right, as in mathematical notation:2 ** 3 ** 2is2 ** 9. Every other binary arithmetic operator groups from the left. - Precedence.
**binds tighter than*,/and%, so3 * 2 ** 2is 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) ** 2or-(2 ** 2). The error message above is V8’s wording. The exponent side is not restricted:2 ** -1is fine. - Numbers.
**andMath.powuse the same algorithm in the spec (Number::exponentiate), so they return the same values. Fractional powers of negative bases areNaN, because the real root is not computed.NaN ** 0is1, while1 ** InfinityisNaN, kept for backward compatibility even though IEEE 754 says 1. Results beyond the double range becomeInfinity. - Coercion. Non-BigInt operands are converted with
ToNumeric, so2 ** '3'is 8 and2 ** 'x'isNaN. - 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 aRangeError.Math.pownever accepts BigInts, so**is the only built-in power for them (see BigInt).
Realistic example
Section titled “Realistic example”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 ** 10const 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 Infinity1312134096 536870912 2147483648The 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.
Common mistakes
Section titled “Common mistakes”- Writing
-x ** 2. It is aSyntaxError, not -x². Write-(x ** 2)or(-x) ** 2, whichever you mean. - Using
^for powers.^is bitwise XOR in JavaScript:2 ^ 3is1. - Expecting left-to-right grouping.
a ** b ** cisa ** (b ** c). Add parentheses when you mean(a ** b) ** c. - Taking roots of negative numbers.
(-8) ** (1 / 3)isNaN; useMath.cbrt(-8)for cube roots. - Mixing BigInt and Number.
2n ** 2throws. Convert one side:2n ** BigInt(2)orNumber(2n) ** 2. - Assuming exact results for large powers of Number.
3 ** 40exceedsNumber.MAX_SAFE_INTEGER, so the result is rounded. Use3n ** 40nwhen you need every digit.
When to use it and when not to
Section titled “When to use it and when not to”| 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.
Compatibility
Section titled “Compatibility”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.
Interview questions
Section titled “Interview questions”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.
Key takeaways
Section titled “Key takeaways”**(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.powexactly; for BigInts both operands must be BigInt.
Related pages
Section titled “Related pages”- BigInt: exact integer powers such as
2n ** 64n. - Logical assignment: other compound assignment operators.
- Numeric separators: readable constants such as
10_000in the backoff example. - Octal and binary literals: writing bit patterns directly instead of computing
2 ** n. - Operators: precedence and associativity of every operator.
- Math object:
Math.pow,Math.sqrt,Math.cbrtandMath.exp.
Official references
Section titled “Official references”- ECMAScript Language Specification, Exponentiation Operator (current draft; first published in ECMAScript 2016, 7th edition)
- ECMAScript 2016 Language Specification (ECMA-262, 7th edition), Exponentiation Operator
- ECMAScript Language Specification,
Number::exponentiate - TC39 proposal: Exponentiation operator
- MDN: Exponentiation (
**) - MDN: Exponentiation assignment (
**=) - MDN: Math.pow()