Emmanuel Akinfulubi

JavaScriptMath

Number.MAX_SAFE_INTEGER explained in JavaScript

Intermediate · 3 min read

JavaScript can store whole numbers exactly, but only up to a point. Past that point, it starts skipping numbers: 9007199254740993 quietly becomes 9007199254740992. No error, no warning. Database IDs, payment references and order numbers can be this long, and when two different IDs turn into the same number, one customer can end up seeing another's record.

In this tutorial you will print the limit, Number.MAX_SAFE_INTEGER, find two different whole numbers that compare as equal past it, and see which check tells you a number is still safe. You need to know === and Number.isInteger.

Why there is a limit

A JavaScript number is stored as a double, a format with a fixed amount of space. It has room for 53 binary digits of precision. Every whole number that fits in 53 binary digits is stored exactly. The biggest one is 2 to the 53rd power, minus 1:

JavaScript

console.log(2 ** 53 - 1); // 9007199254740991

That is about nine thousand trillion. Above it, there aren't enough digits to tell every whole number apart. At first only every second whole number can be stored, then every fourth, and so on. The ones in between get rounded to a neighbor.

"Safe" means exact

A safe integer is a whole number that JavaScript stores exactly, so x + 1 really is the next whole number. Number.MAX_SAFE_INTEGER is the biggest one.

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

18 MAX_SAFE_INTEGER, and what breaks past it

fair

Print Number.MAX_SAFE_INTEGER, then show two different integers that compare equal past that limit. Then check what Number.isInteger says about them.

Stuck? Three hints, smallest first
  1. 1. The limit is 2 ** 53 - 1, because a double has 53 bits of mantissa.
  2. 2. Past it, only every second integer is representable, so x + 1 and x + 2 can land on the same double.
  3. 3. Number.isSafeInteger is the test; Number.isInteger answers a different question.

Step 1: print the limit

Step 1

const max = Number.MAX_SAFE_INTEGER;
console.log(max);                 // 9007199254740991
console.log(max === 2 ** 53 - 1); // true

The constant is exactly 2 ** 53 - 1, as promised.

Step 2: step past it

Step 2

const max = Number.MAX_SAFE_INTEGER;
console.log(max + 1); // 9007199254740992
console.log(max + 2); // 9007199254740992
console.log(max + 1 === max + 2); // true

max + 1 is 2 to the 53rd, which can still be stored. max + 2 should be one more, but there is no room for that number, so it rounds to the same value. Two different sums, one answer.

The same thing happens when you add 1 to 2 to the 53rd:

JavaScript

console.log(2 ** 53 === 2 ** 53 + 1); // true

Step 3: a long ID loses its last digit

Now the real-world version. Say an ID arrives as text, '9007199254740993', and your code turns it into a number:

Step 3

console.log(Number('9007199254740993')); // 9007199254740992
console.log(Number('9007199254740993') === Number('9007199254740992')); // true

The last digit changed from 3 to 2. Now two different IDs are equal. JSON.parse does the same when it reads a long number from a server response:

JavaScript

console.log(JSON.parse('{"id":9007199254740993}').id); // 9007199254740992

The fix is to keep IDs as strings. The string '9007199254740993' keeps every one of its 16 digits, and comparing two strings is exact.

Step 4: isInteger is the wrong check

The natural wrong turn is to reach for Number.isInteger:

Step 4

console.log(Number.isInteger(2 ** 60)); // true

2 to the 60th is a whole number, so isInteger says yes. But it is far past the safe limit, so the numbers around it can't all be stored. isInteger answers "is this value whole?", not "is it exact?".

Step 5: isSafeInteger draws the line

Step 5

console.log(Number.isSafeInteger(2 ** 53 - 1)); // true
console.log(Number.isSafeInteger(2 ** 53));     // false
console.log(Number.isSafeInteger(2 ** 60));     // false

Number.isSafeInteger is true only for whole numbers up to the limit (and down to -Number.MAX_SAFE_INTEGER on the negative side). Use it to check that a count, a total in kobo or an ID read from somewhere is still exact.

When you really need bigger whole numbers, JavaScript has a separate type for that, BigInt, which is the next exercise.

The full solution

The answer panel prints the limit, steps past it, turns the 16-digit ID into a number, and compares isSafeInteger with isInteger. The checks underneath confirm that two different IDs collide and that the string keeps all 16 digits. Press Run to see the output.

The answer

const max = Number.MAX_SAFE_INTEGER;

console.log('MAX_SAFE_INTEGER :', max);
console.log('is 2 ** 53 - 1   :', max === 2 ** 53 - 1);
console.log('max + 1          :', max + 1);
console.log('max + 2          :', max + 2, '<- the same double');
console.log('2 ** 53 + 1      :', 2 ** 53 + 1, '<- did not move');
console.log('a 16-digit id    :', Number('9007199254740993'));
console.log('isSafeInteger    :', Number.isSafeInteger(2 ** 53 - 1), Number.isSafeInteger(2 ** 53));
console.log('isInteger(2**60) :', Number.isInteger(2 ** 60), '<- still whole, just not safe');

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 safe limit', max, 9007199254740991);
test('which is 2 ** 53 - 1', max === 2 ** 53 - 1, true);
test('one past it, adding 1 and adding 2 give the same number', max + 1 === max + 2, true);
test('and 2 ** 53 equals itself plus one', (2 ** 53) === (2 ** 53) + 1, true);
test('isSafeInteger draws the line in the right place', [Number.isSafeInteger(2 ** 53 - 1), Number.isSafeInteger(2 ** 53)], [true, false]);
test('a 16-digit id read as a number loses its last digit', Number('9007199254740993'), 9007199254740992);
test('so two distinct ids can collide', Number('9007199254740993') === Number('9007199254740992'), true);
test('isInteger still says yes, because the value is whole', Number.isInteger(2 ** 60), true);
test('the string form, by contrast, keeps every digit', '9007199254740993'.length, 16);

A double carries 53 bits of mantissa, so every integer up to 253 is representable and beyond that the gaps grow: first every second integer, then every fourth. Nothing throws. max + 1 === max + 2 is the failure in its clearest form.

This is why identifiers from a database, a payment reference or a Twitter-style snowflake id should be carried as strings. JSON.parse will happily turn 9007199254740993 into a number and lose the last digit, and the two ids in the assertion then compare equal — a bug that shows up as one customer seeing another's record. Keep ids as text, or use BigInt (exercise 19).

Common mistakes

  • Storing long IDs as numbers. A 16-digit ID can lose its last digit. Keep IDs as strings.
  • Checking with Number.isInteger. It says true for 2 ** 60, which is not exact. Use Number.isSafeInteger.
  • Expecting an error. Going past the limit never throws. The numbers are just quietly wrong.

Try it yourself

  1. Print 2 ** 53 + 2 and 2 ** 53 + 3. Which one comes out as you expect?
  2. Write safeAdd(a, b) that returns the sum, or throws an error if the result isn't a safe integer.
  3. Multiply 99,999,999 by 99,999,999 and check the answer with Number.isSafeInteger.

Keep practising

Newsletter

New writing, when it lands.

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