Sub topic 6
The Safe Integer Range
This chapter assumes you know that every JavaScript Number is a 64-bit IEEE 754 double. You should also be comfortable running snippets in a browser console or Node.js. Here you will see where integer precision ends and what goes wrong past that point.
Why integers have a limit
A double stores a sign, an exponent, and a 52-bit fraction. A hidden leading bit makes that 53 bits of significand. So any integer that fits in 53 bits is stored exactly. Past that point, the exponent grows and the gap between neighboring representable values widens to 2, then 4, then 8, and so on.
JavaScript names the boundary:
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991 (2 ** 53 - 1)
console.log(Number.MIN_SAFE_INTEGER); // -9007199254740991
console.log(Number.MAX_SAFE_INTEGER === 2 ** 53 - 1); // trueAn integer is "safe" when it is stored exactly and cannot be confused with any other integer. 2 53 itself is exact. It is not safe, because 2 53 + 1 rounds to the same value.
What breaks past the boundary
console.log(2 ** 53 === 2 ** 53 + 1); // true
console.log(9007199254740993); // 9007199254740992
console.log(2 ** 53 + 2); // 9007199254740994
console.log(Number.MAX_SAFE_INTEGER + 2); // 9007199254740992No error is thrown. The literal or the result rounds to the nearest representable double, and ties go to the value with an even significand. Your code keeps running with a wrong number.
| Range of magnitude | Gap between representable values |
|---|---|
| 2^52 to 2^53 | 1 |
| 2^53 to 2^54 | 2 |
| 2^54 to 2^55 | 4 |
| 2^55 to 2^56 | 8 |
Common errors
Large IDs from JSON. Databases and APIs often use 64-bit integer IDs. JSON.parse turns them into Numbers, and they get corrupted without any warning:
const data = JSON.parse('{"id": 9007199254740993}');
console.log(data.id); // 9007199254740992, which is a different recordThe usual fix is to have the API send such IDs as strings and to keep them as strings in your code.
Overflowing results. Both inputs can be safe while their product or sum is not. Multiplying timestamps or summing counters can quietly cross 2^53. Check the result as well as the inputs:
function safeMultiply(a, b) {
const result = a * b;
if (!Number.isSafeInteger(result)) {
throw new RangeError(`Result ${result} is outside the safe integer range`);
}
return result;
}
console.log(safeMultiply(1e6, 1e6)); // 1000000000000
try {
safeMultiply(1e9, 1e9);
} catch (e) {
console.log(e.message); // Result 1000000000000000000 is outside the safe integer range
}The next chapter covers Number.isSafeInteger and its companion checks in detail. When you need exact integers beyond this range, the BigInt chapter shows the alternative.
Exercise
What does console.log(Number.MAXSAFEINTEGER + 2) print, and why?
Show answer
It prints 9007199254740992. The exact sum, 9007199254740993, is above 2^53, where only even integers can be represented. It sits halfway between 9007199254740992 and 9007199254740994, and the tie goes to the value with an even significand, which is 9007199254740992.
Exercise
An API returns {"orderId": 12345678901234567}. Why might two different orders look identical in your JavaScript code, and how do you prevent it?
Show answer
12345678901234567 is larger than Number.MAXSAFEINTEGER, so JSON.parse rounds it to the nearest representable double, and nearby IDs can round to the same value. Have the API send the ID as a string and keep it as a string, or parse it into a BigInt if you need to do arithmetic on it.