Emmanuel Akinfulubi

JavaScriptMath

Infinity, overflow and underflow in JavaScript

Intermediate · 3 min read

Numbers in JavaScript don't go on forever. There is a biggest number you can store and a smallest positive one. Go past the top and you get Infinity. Go below the bottom and you get 0. Neither throws an error, so a calculation can quietly produce nonsense.

In this tutorial you will find both edges, push past them, and see how Infinity and NaN behave in sums and in JSON. You need to know console.log, === and Number.isNaN. Scientific calculations, games and anything that multiplies big numbers together can hit these limits.

Numbers with an exponent

Very large and very small numbers print in exponential notation. 1.5e+308 means 1.5 times 10 to the 308th power: 1.5 followed by 308 digits. 5e-324 means 5 divided by 10 to the 324th: a decimal point, 323 zeros, then 5.

Overflow and underflow

  • Overflow is when a result is too big to store. JavaScript turns it into Infinity.
  • Underflow is when a positive result is too small to store. JavaScript turns it into 0.

Both happen silently. There is no error message, just a different value from the one you expected.

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

16 Overflow to Infinity, underflow to zero

fair

Find the largest and smallest positive doubles, then push past both. Then check how Infinity and NaN behave in arithmetic.

Stuck? Three hints, smallest first
  1. 1. Number.MAX_VALUE and Number.MIN_VALUE are the outer edges, and MIN_VALUE is not negative.
  2. 2. Overflow gives Infinity silently; underflow gives 0 just as silently.
  3. 3. JSON.stringify(Infinity) is "null", so assert it with === Infinity.

Step 1: the largest number

Step 1

console.log(Number.MAX_VALUE);     // 1.7976931348623157e+308
console.log(Number.MAX_VALUE * 2); // Infinity

Number.MAX_VALUE is the biggest finite number JavaScript can store. Double it and there is nowhere to put the answer, so you get Infinity.

Powers of two show exactly where the edge is:

JavaScript

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

Step 2: the smallest positive number

Here is the wrong turn: many people guess Number.MIN_VALUE is the most negative number. It isn't. It is the smallest number above zero:

Step 2

console.log(Number.MIN_VALUE);     // 5e-324
console.log(Number.MIN_VALUE / 2); // 0

Halve it and the answer is too small to store, so it becomes plain zero. The most negative number is simply -Number.MAX_VALUE.

Step 3: Infinity in arithmetic

Infinity behaves like a number in most sums, which is why it can travel a long way through your code before you notice:

Step 3

console.log(Infinity + 1);        // Infinity
console.log(1 / Infinity);        // 0
console.log(Infinity - Infinity); // NaN

Adding to infinity leaves infinity. Dividing by it gives zero. But "infinity minus infinity" has no sensible answer, so it gives NaN, "Not a Number".

Step 4: dividing by zero

In many languages, dividing by zero crashes the program. In JavaScript it doesn't:

Step 4

console.log(1 / 0);  // Infinity
console.log(1 / -0); // -Infinity
console.log(0 / 0);  // NaN

JavaScript has a "minus zero", -0, and dividing by it gives minus infinity. 0 / 0 has no answer, so it is NaN.

Step 5: checking for Infinity and NaN

You can test for infinity with ===, and for NaN with Number.isNaN:

Step 5

console.log(Number.MAX_VALUE * 2 === Infinity);     // true
console.log(Number.isNaN(Infinity - Infinity));     // true
console.log(Number.isFinite(Number.MAX_VALUE * 2)); // false

Number.isFinite is the handy one: it is false for Infinity, -Infinity and NaN, and true for every ordinary number. After a long chain of multiplications, one Number.isFinite check catches both problems.

Don't test for NaN with ===. NaN === NaN is false, because NaN is never equal to anything, even itself.

Step 6: what JSON does with them

JSON is the text format used to send data between programs, for example from a server to a web page. It has no way to write Infinity or NaN:

Step 6

console.log(JSON.stringify({ big: Infinity, bad: NaN })); // {"big":null,"bad":null}

Both quietly become null. The program on the other end sees a missing value instead of a broken one. If your data might contain them, check with Number.isFinite before you send it.

The full solution

The answer panel prints both edges, pushes past them, shows 2 ** 1023 and 2 ** 1024, and sends Infinity and NaN through JSON. The checks underneath confirm each result, using === Infinity and Number.isNaN rather than comparing printed text. Press Run to see the output.

The answer

console.log('MAX_VALUE      :', Number.MAX_VALUE);
console.log('doubled        :', Number.MAX_VALUE * 2);
console.log('MIN_VALUE      :', Number.MIN_VALUE, '<- smallest positive, not most negative');
console.log('halved         :', Number.MIN_VALUE / 2, '<- gone');
console.log('2 ** 1023      :', 2 ** 1023);
console.log('2 ** 1024      :', 2 ** 1024);
console.log('through JSON   :', JSON.stringify({ big: Infinity, bad: NaN }));
console.log('Infinity - Infinity:', Infinity - Infinity);

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('the largest finite double', Number.MAX_VALUE, 1.7976931348623157e+308);
test('doubling it overflows silently to Infinity', Number.MAX_VALUE * 2 === Infinity, true);
test('the smallest positive double is denormal', Number.MIN_VALUE, 5e-324);
test('halving that underflows to plain zero', Number.MIN_VALUE / 2, 0);
test('2 ** 1024 is already past the top', 2 ** 1024 === Infinity, true);
test('while 2 ** 1023 is still finite', Number.isFinite(2 ** 1023), true);
test('Infinity minus Infinity is NaN, not 0', Number.isNaN(Infinity - Infinity), true);
test('1 / Infinity is 0, and 1 / -0 is -Infinity', [1 / Infinity, 1 / -0 === -Infinity], [0, true]);
test('JSON turns both Infinity and NaN into null', JSON.stringify({ big: Infinity, bad: NaN }), '{"big":null,"bad":null}');

Both edges fail quietly. Overflow gives Infinity, which then propagates through every later operation; underflow gives 0, which usually looks like a legitimate answer and only shows up when something divides by it. Neither throws. Number.isFinite after a chain of multiplications is cheap insurance.

The JSON assertion is the one that catches people in an API. Infinity and NaN are not JSON values, so they serialise as null, and the receiving end sees a missing number rather than a broken one. That is also why every assertion in this section reaches for === Infinity or Number.isNaN: through JSON.stringify, which is how test compares, both would otherwise match anything else that serialises to null.

Common mistakes

  • Thinking Number.MIN_VALUE is negative. It is the smallest positive number, 5e-324.
  • Expecting dividing by zero to throw. 1 / 0 is Infinity and 0 / 0 is NaN. Check the divisor yourself.
  • Sending Infinity or NaN as JSON. Both turn into null. Validate with Number.isFinite first.

Try it yourself

  1. Find the smallest whole number n for which 10 ** n is Infinity.
  2. Write safeDivide(a, b) that returns null instead of Infinity or NaN.
  3. Predict Infinity * 0 and -Infinity * -1, 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.