How to check if a Value is an Integer in JavaScript


Borislav Hadzhiev

Last updated: Oct 16, 2021


Photo from Unsplash

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 #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.