JavaScript Number isInteger() method is used to check whether the passed value is an integer.
This method is useful for type checking and validation.
Syntax
Number.isInteger(value)
Parameters
value: It is a value that needs to be tested.
Return Value
Returns true if the provided value is an integer; otherwise, false.
If the value is NaN or Infinity, it returns false.
Visual Representation
Example 1: Basic Usage
console.log(Number.isInteger(20)); //positive number
console.log(Number.isInteger(-20)); //negative number
console.log(Number.isInteger(0));
Output
true
true
true
Example 2: Passing floating value
console.log(Number.isInteger(2.75));
console.log(Number.isInteger(-2.75));
Output
false
false
Example 3: Passing NaN, Infinity, and Boolean values
console.log(Number.isInteger(NaN));
console.log(Number.isInteger(Infinity));
console.log(Number.isInteger(true));
Output
false
false
false
Example 4: Passing string and empty value
console.log(Number.isInteger("Hello"));
console.log(Number.isInteger());
Output
false
false
Browser Compatibility
- Chrome 34 and above
- Edge 12 and above
- Firefox 16 and above
- Safari 9 and above
- Opera 21 and above