Number.isSafeInteger and Safe Integer Range
You'll learn the boundaries within which JavaScript can represent whole numbers with perfect accuracy.
The Safe Integer Range
JavaScript's Number type uses 64-bit floating-point format, which means it can't represent every integer with perfect precision. There's a safe range where integers are guaranteed to be exact: from -(2⁵³ - 1) to (2⁵³ - 1).
That's approximately -9 quadrillion to +9 quadrillion (specifically: -9,007,199,254,740,991 to 9,007,199,254,740,991).
Why This Matters
Think of it like a ruler with markings. Within the safe range, every integer has its own distinct mark. Beyond that range, the marks start to merge—some integers become impossible to represent exactly, and JavaScript may round to the nearest representable value.
Using Number.isSafeInteger
The Number.isSafeInteger() method checks whether a value is:
- An integer (whole number)
- Within the safe range
Number.isSafeInteger(100); // true
Number.isSafeInteger(9007199254740991); // true (MAX_SAFE_INTEGER)
Number.isSafeInteger(9007199254740992); // false (beyond safe range)
Number.isSafeInteger(3.14); // false (not an integer)
The Boundary Constants
JavaScript provides built-in constants for these limits:
Number.MAX_SAFE_INTEGER; // 9007199254740991
Number.MIN_SAFE_INTEGER; // -9007199254740991
These tell you the exact edges of the safe zone. Outside this range, integer math becomes unreliable—you might add 1 to a number and get the same number back!
Key Takeaway: JavaScript integers are only guaranteed accurate between -(2⁵³ - 1) and (2⁵³ - 1); use Number.isSafeInteger() to verify a value falls within this safe range.