Description

The JavaScript Number method isSafeInteger() checks whether the given number is a safe integer.

  • A safe integer is an integer represented as an IEEE-754 double precision number, which has a range between (253 - 1) to -(253 -1).
  • It returns true if the value is a safe integer, otherwise returns false.
  • It is a static method, so it can be directly accessed using the class Number and doesn't need its instance.

Syntax

The method isSafeInteger() has the below syntax, where num is a number.

Number.isSafeInteger(num)

Parameters

The method isSafeInteger() allows the below parameters.

num

  • It is a number to be checked for being a safe integer.

Result

Returns true if the given number is a safe integer, otherwise returns false.

Example 1: Using the Method

The below example shows the basic usage of the method.

// Returns true
console.log(Number.isSafeInteger(123));     // Prints: true
console.log(Number.isSafeInteger(-123));    // Prints: true
console.log(Number.isSafeInteger(3.0));    // Prints: true

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

// Returns false
console.log(Number.isSafeInteger("123"));    // Prints: false
console.log(Number.isSafeInteger(3.01));     // Prints: false

console.log(Number.isSafeInteger(Math.pow(2, 53)));    // Prints: false
console.log(Number.isSafeInteger(-Math.pow(2, 53)));    // Prints: false

console.log(Number.isNaN(Infinity));     // Prints: false
console.log(Number.isNaN(-Infinity));    // Prints: false

Output:

true
true
true
true
true
false
false
false
false
false
false

Overall

The JavaScript Number method isSafeInteger() checks whether the given number is a safe integer.

Related Links