Open In App
Related Articles

JavaScript Number isInteger() Method

Improve Article
Improve
Save Article
Save
Like Article
Like

The Number.isInteger() method in JavaScript is used to check whether the value passed to it is an integer or not. It returns true if the passed value is an integer, otherwise, it returns false.

Syntax:

Number.isInteger(value)

Parameters: This method accepts a single parameter value that specifies the number that the user wants to check for an integer.

Return Value: The number.isInteger() method returns a boolean value,i.e. either true or false. It will return true if the passed value is of the type Number and an integer, else it returns false. 

The below examples illustrate the Number.isInteger() method in JavaScript: 

Example: This example checks for some values if they are integers or not using the Number.isInteger() method in JavaScript.

Javascript




console.log(Number.isInteger(-2));
console.log(Number.isInteger(0));
console.log(Number.isInteger(2));


Output

true
true
true

Passing a negative number as an argument: If a negative integer value is passed to the method as an argument then the method will return true, if the negative value passed to it is not of integer type then the method will return false. 

Example: In this example, a negative number is passed as an argument.

Javascript




console.log(Number.isInteger(-2));
console.log(Number.isInteger(-2.56));


Output

true
false

Passing a positive number as an argument: If a positive integer value is passed to the method as an argument then the method will return true, if the positive value passed to it is not of integer type then the method will return false. 

Example: In this example, a positive number is passed as an argument.

Javascript




console.log(Number.isInteger(2));


Output

true

Passing zero as an argument: If zero is passed to the Number.isInteger() method then it will return true as zero is also an integer. 

Example: In this example, 0 is passed as an argument.

Javascript




console.log(Number.isInteger(0));


Output

true

Passing a number consisting of decimal places as an argument: If a decimal number is passed as the argument, the method will return false. 

Example: In this example, a decimal number is passed as an argument.

Javascript




console.log(Number.isInteger(2.03));


Output

false

Passing a string as an argument: If the argument is passed to the Number. integer () method is of type string then it will return false. 

Example: In this example, a string is passed as an argument.

Javascript




console.log("Output : " + Number.isInteger("hi"));


Output

Output : false

We have a complete list of Javascript Number Methods, to check those please go through the Javascript Number Complete Reference article.

Supported Browsers:

  • Google Chrome 34 and above
  • Edge 12 and above
  • Firefox 16 and above
  • Apple Safari 9 and above
  • Opera 21 and above

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 08 Aug, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials