Description

The JavaScript Number method isInteger() checks whether the given number is an integer.

  • This method is static, so it can be directly accessed using the class Number and doesn't need its instance.
  • It returns true if the number is an integer, otherwise returns false.

Syntax

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

Number.isInteger(num)

Parameters

The method isInteger() allows the below parameters.

num

  • Number to be checked for being an integer.
  • If the value is NaN or Infinity, the method returns false.

Result

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

Example 1: Using the Method

The below example shows the basic usage of the method.

// Returns true
console.log(Number.isInteger(0));          // Prints: true
console.log(Number.isInteger(12));         // Prints: true
console.log(Number.isInteger(-12));        // Prints: true
console.log(Number.isInteger(12.0));       // Prints: true
console.log(Number.isInteger(12.00));      // Prints: true

// Returns false
console.log(Number.isInteger(12.3));          // Prints: false
console.log(Number.isInteger(-12.3));         // Prints: false
console.log(Number.isInteger(Math.PI));       // Prints: false
console.log(Number.isInteger(Infinity));      // Prints: false
console.log(Number.isInteger(NaN));           // Prints: false
console.log(Number.isInteger("12"));          // Prints: false
console.log(Number.isInteger(true));          // Prints: false
console.log(Number.isInteger(false));         // Prints: false
console.log(Number.isInteger([1]));           // Prints: false

Output:

true
true
true
true
true
false
false
false
false
false
false
false
false
false

Overall

The JavaScript Number method isInteger() checks whether the given number is an integer.

Related Links