The isInteger
method can check if a value is an integer value or not.
The syntax of the isInteger()
method is given below.
_.isInteger(value)
This method takes the value to be tested for an integer.
This method returns true
if the provided value is an integer. Otherwise, false
will be returned.
For floating-point numbers that can be represented as integers, true
will be returned. For the value 5.0
, we get true
as a result.
For the argument which is not a type of number, false
is returned. For the String value '5'
, we get false
as a result.
Please note:
false
will be returned forNaN
andInfinity
.
The code demonstrates how to use the isInteger
method in Lodash:
In the above code:
Line 6: We loaded the Lodash
library file from the server link.
Line 10: We used the isInteger
method to check if the numeric value 1
is an integer. We get true
as a result.
Line 13: We used the isInteger
method to check if the float value 10.0
is an integer. The value 10.0
can be represented as an integer, so true
is returned as a result.
Line 16: We used the isInteger
method to check if the float value 10.1
is an integer. The value 10.1
cannot be represented as an integer, so false
is returned as a result.
Line 19: We used the isInteger
method to check if the string value '1'
is an integer. The value '1'
is a string and not a numeric type, so false
is returned.
Line 22: We used the isInteger
method to check if the value NaN
is an integer. The isInteger
method will return false
for NaN
.