JavaScript Number.isSafeInteger()

A safe integer is an integer that can be exactly represented as an IEEE-754 double precision number [ all integers from (253 - 1) to -(253 - 1) ].

The syntax of the isSafeInteger() method is:

Number.isSafeInteger(testValue)

The isSafeInteger() method is called using the Number class name.


Number isSafeInteger() Parameters

The isSafeInteger() method takes in:

  • testValue - The value to be tested for being a safe integer.

Return value from Number isSafeInteger()

  • Returns a Boolean indicating whether or not the given value is a number that is a safe integer (true if safe integer else false).

Example: Using Number.isSafeInteger()

check1 = Number.isSafeInteger(451);
console.log(check1); // true

check2 = Number.isSafeInteger("-451");
console.log(check2); // false

check3 = Number.isSafeInteger(0.6969);
console.log(check3); // false

check4 = Number.isSafeInteger(Math.pow(2, 53));
console.log(check4); // false

check5 = Number.isSafeInteger(Math.pow(2, 53) - 1);
console.log(check5); // true

check6 = Number.isSafeInteger(Infinity);
console.log(check6); // false

check7 = Number.isSafeInteger(3.0);
console.log(check7); // true

Output

true
false
false
false
true
false
true

Recommended Readings: