Skip to main content

isSafeInteger()

isSafeInteger(value): value is number

Checks if value is a safe integer.

A safe integer is an integer that can be exactly represented as an IEEE-754 double precision number, and whose IEEE-754 representation cannot be the result of rounding any other integer to fit the IEEE-754 representation.

DEPRECATED

Use Number.isSafeInteger() directly instead.


Parametersโ€‹

value: unknownโ€‹

The value to check.


Returns: value is numberโ€‹

true if value is a safe integer, else false.


See Alsoโ€‹


Sinceโ€‹

2.0.0


Also known asโ€‹

isSafeInteger (Lodash, es-toolkit) ยท โŒ (Remeda, Radashi, Ramda, Effect, Modern Dash, Antfu)


Exampleโ€‹

// โŒ Deprecated approach
isSafeInteger(42); // => true
isSafeInteger(9007199254740991); // => true (MAX_SAFE_INTEGER)
isSafeInteger(9007199254740992); // => false

// โœ… Recommended approach
Number.isSafeInteger(42); // => true
Number.isSafeInteger(9007199254740991); // => true
Number.isSafeInteger(9007199254740992); // => false

How it works?โ€‹

Checks if value is a safe integer (exactly representable in IEEE-754).

Safe Integer Rangeโ€‹

Common Checksโ€‹

ValueResult
42checkmark true
9007199254740991checkmark true (MAX_SAFE_INTEGER)
9007199254740992cross false
3.14cross false
Infinitycross false

warning Deprecated: Use Number.isSafeInteger() directly.


Use Casesโ€‹

Validate safe integer ๐Ÿ“Œโ€‹

Check if value is a safe integer (within IEEE-754 precision).

Number.isSafeInteger(42);                 // true
Number.isSafeInteger(9007199254740991); // true (MAX_SAFE_INTEGER)
Number.isSafeInteger(9007199254740992); // false

Guard ID operationsโ€‹

Ensure IDs don't lose precision.

function processId(id: number) {
if (!Number.isSafeInteger(id)) {
throw new Error('ID too large, use BigInt');
}
return fetchRecord(id);
}