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.The isInteger() method returns a boolean depending upon the value as follows:
isInteger() method returns false if the value is not an integer.isInteger() method returns true if the value is an integer.The isInteger() method is supported by the following browsers:
Note: The
isInteger()method is not supported by Internet Explorer 11 and earlier versions.
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);
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.
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);
Non-integers are passed to the isInteger() method in the code snippet above. The isInteger() method returns false for the non-integers.
Free Resources