Exponentiation in JavaScript (** operator, Math.pow, tested output)

Tech reviewed: Deepak Prasad
Exponentiation in JavaScript (** operator, Math.pow, tested output)

Exponentiation means raising a base to an exponent—for example, raising 4 to the third power yields 64. In modern JavaScript you usually express that with the ** operator, or with Math.pow(base, exponent) when you want a function call or need compatibility with very old engines. For more patterns around powers and edge cases, see JavaScript Math.pow and related topics.

This guide covers **, Math.pow, BigInt rules, right-associativity, the unary-minus gotcha, and a couple of edge cases from the spec.

Tested on: all examples in this tutorial were run with Node.js v20.18.2, and the output shown below each snippet is its exact console output.


Quick reference

Use this table for exponentiation javascript: numbers vs BigInt and parsing pitfalls.

Tool Numbers BigInt Notes
x ** y Yes Yes if both are BigInt Right-associative; unary - needs () on base
Math.pow(x, y) Yes No (throws on BigInt) Works on older engines without **

1. From repeated multiplication to **

javascript
let repeated = 4 * 4 * 4;
let operator = 4 ** 3;
console.log(repeated);
console.log(operator);
text
64
64

2. Exponentiation (**) and BigInt

The operator is overloaded for number and BigInt (see MDN): if both sides are BigInt, you get a BigInt result. Math.pow, by contrast, only accepts numbers—passing a BigInt throws TypeError.

javascript
const big = BigInt(345) ** BigInt(3);
console.log(big);
text
41063625n
javascript
try {
  console.log(Math.pow(2n, 2));
} catch (e) {
  console.log(e.name + ": " + e.message.split("\n")[0]);
}
text
TypeError: Cannot convert a BigInt value to a number
javascript
try {
  console.log(2n ** 2);
} catch (e) {
  console.log(e.name);
}
text
TypeError

Use 2n ** 2n (or convert explicitly) when you need BigInt powers.


3. Fractional exponents (still numbers)

javascript
console.log(4 ** 3.3);
text
97.00586025666546

Floating-point javascript exponent math is approximate, like other Number operations.


4. Negative base: parenthesize

Unary - binds looser than **, so -2 ** 5 is parsed as -(2 ** 5), not (-2) ** 5. Use parentheses on the base when you mean a negative number raised to a power.

javascript
console.log((-2) ** 5);
text
-32

5. Right-associativity (**)

a ** b ** c is a ** (b ** c), not (a ** b) ** c.

javascript
console.log(2 ** 3 ** 2);
console.log(2 ** (3 ** 2));
console.log((2 ** 3) ** 2);
text
512
512
64

6. NaN ** 0 (special case)

javascript
console.log(NaN ** 0);
text
1

The same quirk applies to Math.pow(NaN, 0) per MDN.


7. Compound assignment (**=)

javascript
let x = 2;
x **= 3;
console.log(x);
text
8

8. Math.pow (numbers only)

javascript
console.log(Math.pow(4, 5));
console.log(Math.pow(6, -2));
console.log(Math.pow(-2, 5));
text
1024
0.027777777777777776
-32

Math.pow(-2, 5) does not need extra parentheses on the base because the base is a normal function argument, not a unary-expression parsing edge case.


Summary

  • ** is concise and supports BigInt when both operands are BigInt; watch precedence with unary -.
  • ** is right-associative: add parentheses when you mean (a ** b) ** c.
  • Math.pow is numbers-only; use it when targeting engines without **.
  • NaN ** 0 and Math.pow(NaN, 0) yield 1 by spec.

References

MDN references for the exponentiation operator, Math.pow, and precedence.


Frequently Asked Questions

1. What is the javascript power operator?

The infix operator ** raises the left operand to the right operand, for example 2 ** 10 === 1024. It was added in ES2016 and is documented on MDN as the exponentiation operator.

2. Should I use ** or Math.pow for exponentiation javascript code?

For plain numbers they are equivalent for many cases. Use ** for concise syntax and BigInt exponentiation when both operands are BigInts. Use Math.pow when you must support environments so old they lack **, or when you prefer a function call style.

3. Can I mix BigInt and number with **?

No. If one operand becomes a BigInt and the other a number after coercion, JavaScript throws TypeError. Use explicit conversions such as Number(2n) ** 2 or 2n ** 2n.

4. Why does -2 ** 5 throw or parse oddly?

Unary minus has lower precedence than **, so -2 ** 5 is parsed as -(2 ** 5). To raise a negative base, parenthesize the base: (-2) ** 5.

5. Is 2 ** 3 ** 2 equal to 64?

No. ** is right-associative, so 2 ** 3 ** 2 is 2 ** (3 ** 2) === 2 ** 9 === 512. Use parentheses (2 ** 3) ** 2 for 64.

6. What is NaN ** 0 in JavaScript?

NaN ** 0 and Math.pow(NaN, 0) both return 1; it is a special case where NaN does not propagate.
Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital …

  • JavaScript
  • Web Design