JavaScript Number isInteger() Method
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));
|
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));
|
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" ));
|
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
`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.
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.
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
Share your thoughts in the comments
Please Login to comment...