Just in case you didn’t know: all
2^53 - 1. (This is also available as a static property on
Number.MAX_SAFE_INTEGER.) This is not as high as one might expect if one isn’t already familiar with
binary64 numbers. One can test a number to see if it’s still “safe” to use with
The reason the numbers are considered “unsafe” is because doing arithmetic or comparisons with the number will yield unexpected results. Once a number has grown above the point of safety, that number can never be trusted again. Here is an example:
const a = Number.MAX_SAFE_INTEGER + 1000 const b = a - Number.MAX_SAFE_INTEGER b === 1000 // false
We would expect
Number.MAX_SAFE_INTEGER + 1000 - Number.MAX_SAFE_INTEGER to be
1000 if the world made sense, but sadly it doesn’t. Once a number has crossed the “safe” threshold, it can’t ever be trusted again, even if one tries to subtract it back down into the safe range.
Also, and amazingly, this simple test will evaluate as
Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 // true
What can one do?
Well, there is a new
The other option is to try to never let the numbers get too large by capping them and/or testing for safety where needed. This is what I generally do.
Good luck and I hope you learned something.