Open In App

JavaScript Number isInteger() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The JavaScript Number isInteger() method checks if a value is an integer, returning true if it is, otherwise false.

Number isInteger() Syntax

Number.isInteger(value);
  • value: The value to be checked.

Return Value

It 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, it returns false. 

Number isInteger() 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


Explanation

The code uses Number.isInteger() to check if the given values (-2, 0, 2) are integers. It logs `true` for each value because they are all integers.

Number isInteger() Example

Here, a negative number is passed as an argument.

Javascript




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


Output

false

Explanation

This code logs `false` because `-2.56` is not an integer. `Number.isInteger()` returns `false` for non-integer numeric values.

Number isInteger() Example

Here, a string is passed as an argument.

Javascript




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


Output

Output : false


Explanation:

This code logs "Output : false" because "hi" is not a number and Number.isInteger() returns false for non-numeric values.

JavaScript Number isInteger() Method UseCase

1. How to compare isNaN() and isInteger() Methods in JavaScript ?

`isNaN()` checks if a value is not a number, while `isInteger()` checks if a value is an integer. They serve different purposes: `isNaN()` for general numeric checks and `isInteger()` for specifically checking integers.

2. How to Check if a Value is a Number in JavaScript ?

To check if a value is a number in JavaScript using the isInteger method, you can use Number.isInteger(value), returning true if it’s an integer, false otherwise.

3. JavaScript Program to Check if a Number is Float or Integer

In JavaScript, you can check if a number is a float or an integer using the Number.isInteger() method, which returns true for integers and false for floats.

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

Supported Browsers:



Last Updated : 04 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads