Emmanuel Akinfulubi

JavaScriptMath

Exponents with ** and Math.pow in JavaScript

Beginner · 4 min read

Raising a number to a power means multiplying it by itself a number of times. 2 to the power 10 is 2 multiplied by itself ten times, which is 1024. Powers show up in compound interest, in file sizes (a kilobyte is 2 to the 10th bytes), in growth charts and in every distance formula.

In this tutorial you will raise numbers to whole, fractional, negative and zero powers in JavaScript. You will learn which way 2 ** 3 ** 2 is worked out, and find the exact point where a power of two gets too big and becomes Infinity. You only need to know how to print a value with console.log.

Two ways to write a power

JavaScript has an operator for powers, **, and an older function, Math.pow. They do the same thing:

JavaScript

console.log(2 ** 10);        // 1024
console.log(Math.pow(2, 10)); // 1024

The number being multiplied is the base (here 2). The small raised number is the exponent (here 10). Most modern code uses ** because it reads like the maths.

Here is the exercise. Try it yourself first if you like, then read on and we will build it together.

27 Powers, with ** and Math.pow

easy

Raise numbers to integer, fractional, negative and zero exponents. Then find the exponent at which a power of two overflows to Infinity.

Stuck? Three hints, smallest first
  1. 1. a ** b and Math.pow(a, b) are the same operation.
  2. 2. A fractional exponent is a root, and a negative exponent is a reciprocal.
  3. 3. A double runs out at about 1.8e308, so somewhere near 2 ** 1024 there is a cliff.

Step 1: fractional exponents are roots

An exponent doesn't have to be a whole number. Raising to the power one half gives the square root. Raising to the power one third gives the cube root:

Step 1

console.log(9 ** 0.5);      // 3
console.log(27 ** (1 / 3)); // 3

The brackets around 1 / 3 matter. Without them, 27 ** 1 is worked out first and then divided by 3, giving 9.

Step 2: negative exponents are fractions

A negative exponent means "one divided by the positive power". This is called a reciprocal:

Step 2

console.log(2 ** -2); // 0.25
console.log(1 / 2 ** 2); // 0.25

2 to the -2 is 1 divided by 2 squared, which is 1 / 4, or 0.25.

Step 3: the zero exponent

Anything to the power 0 is 1:

Step 3

console.log(5 ** 0); // 1
console.log(0 ** 0); // 1

0 ** 0 is the one people argue about. Mathematicians debate it, but JavaScript's rules say it is 1, so that is what you get.

Step 4: which way it groups

Most operators are worked out from left to right. ** is the exception: it groups from the right. This is called being right associative.

Step 4

console.log(2 ** 3 ** 2);   // 512
console.log(2 ** (3 ** 2)); // 512: the same thing
console.log((2 ** 3) ** 2); // 64: left first gives a different answer

2 ** 3 ** 2 means 2 to the power 9, not 8 squared. If you mean the other order, add brackets.

JavaScript also refuses to guess when a minus sign sits right before a base. -2 ** 2 is a syntax error, so the code won't run at all. Write (-2) ** 2 (which is 4) or -(2 ** 2) (which is -4).

Step 5: the overflow cliff

Numbers in JavaScript are stored as 64-bit floating-point values, often called doubles. The largest one is about 1.8 times 10 to the 308th. Go past it and the result becomes Infinity:

Step 5

console.log(2 ** 1023); // 8.98846567431158e+307
console.log(2 ** 1024); // Infinity

The e+307 is scientific notation: 8.988 times 10 to the 307th. One step further, and the value no longer fits. No error is thrown, and any later maths on Infinity stays Infinity. If your code computes large powers, for example growth over many years, check the result with Number.isFinite.

Step 6: negative base, fractional exponent

The cube root of -8 is -2, since -2 times -2 times -2 is -8. But ** doesn't give it:

Step 6

console.log((-8) ** (1 / 3)); // NaN
console.log(Math.cbrt(-8));   // -2

** only accepts a negative base with a whole-number exponent. With a fraction, it returns NaN ("Not a Number"). That isn't a bug: in general a negative number raised to a fraction has no single real answer, so the rule refuses to pick one. Math.cbrt exists to give the real cube root, and it handles negatives.

The full solution

The answer panel prints each kind of power: whole, fractional, negative, zero, right-grouped, the overflow at 1024 and the negative base. The checks underneath confirm every value, including that 2 ** 1024 === Infinity and that Math.cbrt(-8) is -2. Press Run to see the output.

The answer

console.log('2 ** 10        :', 2 ** 10, 'and Math.pow:', Math.pow(2, 10));
console.log('27 ** (1/3)    :', 27 ** (1 / 3), '<- a root');
console.log('2 ** -2        :', 2 ** -2, '<- a reciprocal');
console.log('anything ** 0  :', 0 ** 0, 5 ** 0);
console.log('2 ** 3 ** 2    :', 2 ** 3 ** 2, '<- right associative');
console.log('2 ** 1023      :', 2 ** 1023);
console.log('2 ** 1024      :', 2 ** 1024, '<- over the edge');
console.log('(-8) ** (1/3)  :', (-8) ** (1 / 3), '<- no real answer by this route');

Output


      
9 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('** and Math.pow agree', [2 ** 10, Math.pow(2, 10)], [1024, 1024]);
test('a fractional exponent is a root', 27 ** (1 / 3), 3);
test('a negative exponent is a reciprocal', 2 ** -2, 0.25);
test('anything to the power 0 is 1, including 0', [0 ** 0, 5 ** 0], [1, 1]);
test('** associates to the right', 2 ** 3 ** 2, 512);
test('2 ** 1023 is finite', 2 ** 1023, 8.98846567431158e+307);
test('2 ** 1024 overflows to Infinity', 2 ** 1024 === Infinity, true);
test('a negative base with a fractional exponent is NaN', Number.isNaN((-8) ** (1 / 3)), true);
test('so use Math.cbrt for an odd root of a negative', Math.cbrt(-8), -2);

(-8) ** (1/3) being NaN is not a bug: the operator is defined for a negative base only with an integer exponent, because in general there is no single real answer — the cube root of -8 has three complex solutions and IEEE-754 declines to choose. Math.cbrt exists precisely to give the real one.

0 ** 0 is 1 by specification. Mathematicians argue about it; IEEE-754 and JavaScript do not.

The overflow cliff is silent. 2 ** 1024 is Infinity, and any later arithmetic keeps it, so a compound-growth calculation with a large exponent should be checked with Number.isFinite rather than trusted.

Common mistakes

  • Writing 27 ** 1 / 3 for a cube root. That is 27 divided by 3. Use 27 ** (1 / 3) or Math.cbrt(27).
  • Reading 2 ** 3 ** 2 left to right. It is 2 to the 9th, 512. Add brackets if you mean 64.
  • Not checking for Infinity. A big power quietly overflows. Test with Number.isFinite.

Try it yourself

  1. Work out how much 1000 naira becomes after 10 years at 5% a year with 1000 * 1.05 ** 10. Round it to 2 decimal places.
  2. Find the smallest whole n where 10 ** n is Infinity.
  3. Predict (-2) ** 3 and (-2) ** 0.5, then run them.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.