How to check if a Value is an Integer in JavaScript


Borislav Hadzhiev

Sat Oct 16 20211 min read


Photo by Chema Photo

Check if a Value is an Integer in JavaScript #

To check if a value is an integer, call the Number.isInteger() method, passing it the value as a parameter. The method returns true if the provided value is an integer, otherwise it returns false.

console.log(Number.isInteger(1)); // 👉️ true console.log(Number.isInteger(1.5)); // 👉️ false console.log(Number.isInteger('1')); // 👉️ false console.log(Number.isInteger(-10)); // 👉️ true

The only parameter the Number.isInteger method takes is the value we want to test for being an integer.

The method returns true if the passed in value:

  • is an integer
  • is a float that can be represented as an integer

Here are examples of floats that can be represented as integers:

const a = 10.0; const b = 10.000000000000000123; console.log(Number.isInteger(a)); // 👉️ true console.log(Number.isInteger(b)); // 👉️ true

If the Number.isInteger method is called with NaN or Infinity, it returns false.

console.log(Number.isInteger(NaN)); // 👉️ false console.log(Number.isInteger(Infinity)); // 👉️ false

Further Reading #

Use the search field on my Home Page to filter through my more than 1,000 articles.