What is isInteger() in Javascript?
isInteger() is a Number method that is used to determine if a given value is an integer. The isInteger() method is declared as follows:
Number.isInteger(value)
value: The value to be tested to check whether it is an integer or not.
Return value
The isInteger() method returns a boolean depending upon the value as follows:
- The
isInteger()method returnsfalseif thevalueis not an integer. - The
isInteger()method returnstrueif thevalueis an integer.
Browser compatibility
The isInteger() method is supported by the following browsers:
- Edge 12
- Firefox 16
- Google Chrome 19
- Opera 22
- Safari 9
Note: The
isInteger()method is not supported by Internet Explorer 11 and earlier versions.
Examples
Example 1
Consider the code snippet below, which demonstrates the use of the isInteger() method:
var foo = Number.isInteger(123)console.log("Given value is integer: ",foo);var foo = Number.isInteger(0)console.log("Given value is integer: ",foo);var foo = Number.isInteger(-123)console.log("Given value is integer: ",foo);
Explanation
Positive, negative, and 0 integers are passed to the isInteger() method in line 1, line 4, and line 7, respectively. The isInteger() method returns true, as the given values are integers.
Example 2
Consider the code snippet below, in which non-integers are passed to the isInteger() method:
var foo = Number.isInteger("123")console.log("Given value is integer: ",foo);var foo = Number.isInteger(11.7)console.log("Given value is integer: ",foo);var foo = Number.isInteger(Infinity)console.log("Given value is integer: ",foo);var foo = Number.isInteger(NaN)console.log("Given value is integer: ",foo);var foo = Number.isInteger(true)console.log("Given value is integer: ",foo);var foo = Number.isInteger(Math.PI)console.log("Given value is integer: ",foo);var foo = Number.isInteger([1])console.log("Given value is integer: ",foo);
Explanation
Non-integers are passed to the isInteger() method in the code snippet above. The isInteger() method returns false for the non-integers.
Free Resources