Check If the Value is an Integer in JavaScript | Plantpot

Check If the Value is an Integer in JavaScript

Web Dev
Sponsored links

In this article, I will introduce how to check if the value is an integer in JavaScript.

The Number.isInteger() method

The Number.isInteger method is a method that determines whether the value passed as an argument is an integer.
This method returns true if the value is an integer, false otherwise.

The syntax is below.

Number.isInteger(value);

Anything with 0 decimal places, such as "1.0", is considered an integer and returns true.

Number.isInteger(1); //true 
Number.isInteger(1.0); //true
Number.isInteger(0.1); //false 
Number.isInteger("string"); //false
Sponsored links

Sample Code

Below is a sample code that gets a value from the input tag and determines if it is an integer.

const input1 = document.getElementById('el');
if(Number.isInteger(input1.value)){
  console.log('The value is an integer.');
}else{
  console.log('The value is not an integer.');
}