Below is the example of Number.isInteger( ) Method.
- Example:
<
script
type
=
"text/javascript"
>
document.write(Number.isInteger(-2)+"<
br
>");
document.write(Number.isInteger(0)+"<
br
>");
document.write(Number.isInteger(2));
</
script
>
chevron_rightfilter_none - Output:
true true true
The Number.isInteger() method in JavaScript is used to check whether the value passed to it is an integer or not. It returns true if the passed value is an integer, otherwise, it returns false.
Syntax:
Number.isInteger(value)
Parameters: This method accepts a single parameter value which specifies the number which the user wants to check for integer.
Return Value: The number.isInteger() method 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, else it returns false.
The below examples illustrates the Number.isInteger() method in JavaScript:
-
Passing a negative number as an argument: If a negative integer value is passed to the method as an argument then the method will return true, if the negative value passed to it is not of integer type then the method will return false.
<
script
type
=
"text/javascript"
>
document.write(Number.isInteger(-2));
document.write(Number.isInteger(-2.56));
</
script
>
chevron_rightfilter_noneOutput:
true false
-
Passing a positive number as an argument: If a positive integer value is passed to the method as argument then the method will return true, if the positive value passed to it is not of integer type then the method will return false.
<
script
type
=
"text/javascript"
>
document.write(Number.isInteger(2));
</
script
>
chevron_rightfilter_noneOutput:
true
-
Passing a zero as an argument: If zero is passed to the Number.isInteger() method then it will return true as zero is also an integer.
<
script
type
=
"text/javascript"
>
document.write(Number.isInteger(0));
</
script
>
chevron_rightfilter_noneOutput:
true
-
Passing a number consisting of decimal places as an argument: If a decimal number is passed as an
the argument, the method will return false.<
script
type
=
"text/javascript"
>
document.write(Number.isInteger(2.03));
</
script
>
chevron_rightfilter_noneOutput:
false
-
Passing a string as an argument: If the argument passed to the Number.isInteger() method is of type string then it will return false.
<
script
type
=
"text/javascript"
>
document.write("Output : " + Number.isInteger("hi"));
</
script
>
chevron_rightfilter_noneOutput:
false
Supported Browsers:
- Google Chrome 19
- Internet Explorer 12
- Firefox 16
- Apple Safari 09
- Opera 22