API · JavaScript

JavaScript Number.isSafeInteger() Method

This article introduces the Number.isSafeInteger(), a JavaScript built-in Number library method used to check whether a numeric value is a safe integer or not.

On this page

In JavaScript, Number.isSafeInteger() checks whether a value is an integer that JavaScript can represent exactly and distinguish from adjacent integers. A safe integer is within the inclusive range -(2^53 - 1) through 2^53 - 1. The fact that JavaScript can represent some numbers outside this range does not make those integers safe: beyond the range, distinct integer values can map to the same IEEE-754 double-precision number.

What Is a Safe Integer?

JavaScript’s Number type uses IEEE-754 double-precision binary floating-point. Within the inclusive range from Number.MIN_SAFE_INTEGER (-9007199254740991) through Number.MAX_SAFE_INTEGER (9007199254740991), every integer is exactly representable and distinguishable from adjacent integers. Some integers outside that range are also representable exactly, but adjacent integers are not always distinguishable. For example, 9007199254740992 is representable, but adding one cannot produce a distinct next integer value. Use the safe-integer range when exact integer comparisons or arithmetic matter, such as when validating an identifier or a count stored as a Number.

Syntax

Call the static method on Number with the value to check: Number.isSafeInteger(value). The argument is the value itself; the method does not parse or otherwise convert it first. It returns a Boolean, so it can be used directly in a condition.

Return Value and Edge Cases

The method returns true only when its argument is a primitive Number whose value is an integer in the inclusive safe range. It returns false for fractions, NaN, positive or negative infinity, and integers outside that range. It also returns false for values of other types, including numeric strings, booleans, null, objects, and BigInt values. In particular, Number.isSafeInteger("42") is false; unlike a parsing function, this method does not coerce the string to the number 42.

The range endpoints are included: both Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER pass. The next integer beyond either endpoint fails. If a value has already been rounded while it was calculated or converted to a Number, this check cannot recover the original integer; validate external input before precision can be lost when exactness is important.

Use the result as one validation condition when a program accepts a count or index. If input starts as text, first decide how that text should be parsed and whether its original spelling matters; parsing can change the value or lose precision before this method sees it. Number.isSafeInteger() answers only whether the current value is a safe integer.

Examples

Run this example:

const values = [
  42,
  Number.MIN_SAFE_INTEGER,
  Number.MAX_SAFE_INTEGER,
  Number.MAX_SAFE_INTEGER + 1,
  3.5,
  Infinity,
  NaN,
  "42",
  {},
];

for (const value of values) {
  console.log(`${String(value)}: ${Number.isSafeInteger(value)}`);
}
42: true
-9007199254740991: true
9007199254740991: true
9007199254740992: false
3.5: false
Infinity: false
NaN: false
42: false
[object Object]: false

The first value is an ordinary safe integer, and the two named constants show that both range limits are accepted. The next value is outside the range; the fraction, infinity, NaN, string, and object are rejected. Because the check does not convert its argument, a numeric string remains a string and returns false.