Check safe integer boundaries
Validate identifiers, counters, timestamps, or other integer inputs before storing them as Number values.
JS Puzzles · Const
Number.MAX_SAFE_INTEGER is the Number static property equal to 9007199254740991 (2^53 − 1). It is the greatest integer JavaScript can represent exactly and compare reliably as a Number.
Number.MAX_SAFE_INTEGER is a static Number property whose value is 9007199254740991, equal to 2^53 − 1. It marks the greatest integer that JavaScript can represent exactly and compare reliably as a Number; calculations beyond it can lose integer precision.
Number.MAX_SAFE_INTEGER9007199254740991
const upper = Number.MAX_SAFE_INTEGER;
const next = upper + 1;
const afterNext = upper + 2;
console.log(upper); // 9007199254740991
console.log(next === afterNext); // trueThe two different additions produce the same Number value because integers beyond the safe limit cannot always be distinguished precisely.
Validate identifiers, counters, timestamps, or other integer inputs before storing them as Number values.
Choose between Number and BigInt when an integer may exceed the exact-precision range.
Explain surprising equality or arithmetic results at very large integer magnitudes.
Number.MAX_SAFE_INTEGER is 9007199254740991, or 2^53 − 1. It is the largest integer that a JavaScript Number can represent exactly and compare correctly.
Follow-up: What can happen above this value? Different integers may round to one Number value, so equality and integer arithmetic can become unreliable; use BigInt for larger integers.
JavaScript Numbers use double-precision floating point, whose mantissa provides 53 bits of integer precision. That makes 2^53 − 1 the largest safely distinguishable integer.
Follow-up: Why does the limit apply to integers rather than all numeric values? Floating-point Numbers can represent much larger magnitudes, but not every integer in those magnitudes exactly.
The property itself is a Number value, so arithmetic beyond it can still produce meaningful approximate results, but integer-level precision is not guaranteed. For exact large-integer calculations, BigInt is the appropriate alternative.
Follow-up: Can Number.MAX_VALUE replace it? No: MAX_VALUE is the largest representable Number magnitude, whereas MAX_SAFE_INTEGER concerns exact integer precision.
Number.MIN_SAFE_INTEGERNumber.isSafeInteger()Number.MAX_VALUENumber.EPSILONBigInt