How to check if a variable is integer JavaScript

Mo Ibra

Mo Ibra

Dec 22, 2022

In this article we will learn how to check if variable is a number.

It is very common to check if variable is a number in JavaScript.


Problem

Now we have a problem, which is that we want check if a variable is number or not.

Now let's imagine we have a variable like this:

let a = 10;

And we want to know if this variable is a number or not.

And if we do that we want the result to be true or false

How to solve this problem?

Well, the solution is easy, especially with the Number.isInteger method.


Check if variable is integer with isInteger method

We can use Number.isInteger to check if variable is number or not.

Example

Number.isInteger(34)         // true
Number.isInteger(2e64)       // true
Number.isInteger('0')        // false

Number.isInteger(null)       // false
Number.isInteger(Infinity)   // false
Number.isInteger(NaN)        // false
Number.isInteger(-Infinity)  // false

The Number.isInteger() method determines whether the passed value is an integer.


Thank you for reading

Thank you for reading my blog. 🚀

Suggestions For More Articles: