JavaScript Number isInteger() Method
The Number.isInteger() method in JavaScript is useful for checking whether a number is a whole number or not.
Syntax:
Number.isInteger(value);
Parameters:
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.
Example 1: This example checks for some values if they are integers or not using the Number.isInteger() method in JavaScript.
console.log(Number.isInteger(-2));
console.log(Number.isInteger(0));
console.log(Number.isInteger(2));
console.log(Number.isInteger(-2));
console.log(Number.isInteger(0));
console.log(Number.isInteger(2));
Output
true true true
Example 2: Here, a negative number is passed as an argument. This code logs `false` because `-2.56` is not an integer. `Number.isInteger()` returns `false` for non-integer numeric values.
console.log(Number.isInteger(-2.56));
console.log(Number.isInteger(-2.56));
Output
false
Example 3: Here, a string is passed as an argument. This code logs "Output : false" because "hi" is not a number and Number.isInteger() returns false for non-numeric values.
console.log("Output : " + Number.isInteger("hi"));
console.log("Output : " + Number.isInteger("hi"));
Output
Output : false