JavaScript Safe Integers: Representability and Boundaries
Quick answer
A safe integer is a Number integer in the range from -(2^53-1) through 2^53-1. Within that interval adjacent mathematical integers are distinguishable.
How it works
Number.isInteger() tests whether the stored Number is integral; Number.isSafeInteger() also checks the safe interval. Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER expose its limits. Some larger integers are representable but are not safe. Check results as well as inputs before relying on exact Number integer arithmetic.
const max = Number.MAX_SAFE_INTEGER;console.log(Number.isSafeInteger(max), Number.isSafeInteger(max+1));Expected output in Node.js:
true falseThe upper endpoint is included; its successor is unsafe.
const n = Number.MAX_SAFE_INTEGER + 1;console.log(Number.isInteger(n), n === n + 1);Expected output in Node.js:
true trueAn integral stored Number can fail to distinguish adjacent integers.
Common mistakes
Converting an already rounded Number into BigInt cannot restore digits that were lost before conversion. Read exact large decimal values as text first.
Practice
Test both safe bounds and their immediate successors.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Safe Integers: Representability and Boundaries — Knowledge Check
Apply the taught concept and predict the output.
Which predicate checks both integrality and the safe Number range?
Is 2 ** 53 representable as Number?
Which input representation best preserves a large exact decimal identifier before conversion?
What is the exact console output in JavaScript Safe Integers: Representability and Boundaries example 1?
const max = Number.MAX_SAFE_INTEGER;console.log(Number.isSafeInteger(max), Number.isSafeInteger(max+1));What is the exact console output in JavaScript Safe Integers: Representability and Boundaries example 2?
const n = Number.MAX_SAFE_INTEGER + 1;console.log(Number.isInteger(n), n === n + 1);