liveBook · Manning

Safe integer

As explained in Math for Web Design

The JavaScript safe integer range is the range of integers that the Number type can represent exactly without loss of precision.

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

How Math for Web Design covers it

How deep it goes
Explains the 53-bit basis of the range, demonstrates boundary failure, and provides Number.isSafeInteger() for validation.
Examples
Demonstrates MAX_SAFE_INTEGER overflow behavior and tests values with Number.isSafeInteger().
What you can do after
Diagnose/debug precision failures and select Number or BigInt according to integer size and UI constraints.
How it connects
Connects Number precision with floating-point representation, BigInt, type checking, JSON, styles, and layout.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math for Web Design. The section links point to the parts of the book it draws on.