Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Number.isSafeInteger: an exact integer in the safe range

Number.isSafeInteger returns true for an integral Number from -(2^53−1) through 2^53−1, where binary64 preserves integer precision.

Version: ECMAScript 2015 onward; example target: Node.js 24.22.2

Syntax

Number.isSafeInteger(number)

Number.isSafeInteger(value) requires a finite, integral value of Number type and Math.abs(value) <= Number.MAX_SAFE_INTEGER. Thus Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER pass, but values outside that range do not; +0 and -0 are safe. NaN, infinities, and Numbers with a represented fractional part produce false.

The method performs no coercion, so strings, BigInt, Symbol, objects, and new Number(...) wrappers produce false. It checks the already stored binary64 value, not original text before parsing. A safe current operand does not guarantee that the next operation is safe: adding 1 to Number.MAX_SAFE_INTEGER leaves the range, after which distinct mathematical integers can round to the same Number.

The method has existed since ECMAScript 2015 and is not a constructor. The example increments a non-negative sequence number only when the result will remain safe, rejecting the boundary value. For counters, identifiers, or amounts beyond the range, use BigInt or a decimal representation; BigInt is exact, but Number.isSafeInteger intentionally returns false for it.

Examples

function nextSequence(value) {
  if (!Number.isSafeInteger(value) || value < 0 || value >= Number.MAX_SAFE_INTEGER) return null;
  return value + 1;
}

console.log(nextSequence(41), nextSequence(Number.MAX_SAFE_INTEGER));
console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER), Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1));
console.log(Number.isSafeInteger(-0), Number.isSafeInteger("42"), Number.isSafeInteger(42n));

Result

42 null
true false
true false false

Number.isSafeInteger → safely incrementing a sequence number

Sources