A safe integer is an integer that JavaScript’s Number type can represent exactly. The safe range runs from -9,007,199,254,740,991 through 9,007,199,254,740,991. Beyond those limits, nearby integers may become indistinguishable and calculations can silently produce unexpected results. section 2.2.1
Why the boundary matters
Number.MAX_SAFE_INTEGER + 1 evaluates to 9007199254740992, and adding 2 produces the same value. JavaScript can store both results, but Number no longer distinguishes them reliably. section 2.2.1
Use the safe-integer boundary as a practical check:
- Use Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER when inspecting the limits of Number. section 2.2.1
- Call Number.isSafeInteger(value) to test whether a number is an exactly representable integer. section 2.2.1
- Choose BigInt for integers beyond the safe range, but avoid it when values must drive animations, styles, layout, JSON, or localStorage. section 2.4.4
Listing 2.1 Working with unsafe integers
console.log(`The maximum safe integer is ${Number.MAX_SAFE_INTEGER}`);
const unsafeNum1 = Number.MAX_SAFE_INTEGER + 1;
const unsafeNum2 = Number.MAX_SAFE_INTEGER + 2;
console.log(`The maximum safe integer plus 1 is ${unsafeNum1}`);
console.log(`The maximum safe integer plus 2 is ${unsafeNum2}`);
Run the boundary calculation to see two different inputs collapse to the same unsafe Number value. section 2.2.1
