The isInteger method in JavaScript assesses whether a specified value qualifies as an integer. It yields true if the provided value is indeed an integer; if not, it returns false.
Syntax
The syntax for the isInteger method is outlined as follows:
Example
Number.isInteger(num)
Parameter
num - A number to be checked.
Return
A Boolean value.
JavaScript Number isInteger method example
In this section, we will explore the isInteger method by examining a variety of examples.
Example 1
Let's see a simple example of isInteger method.
Example
<script>
var x=0;
var y=1;
var z=-1;
document.writeln(Number.isInteger(x));
document.writeln(Number.isInteger(y));
document.writeln(Number.isInteger(z));
</script>
Output:
Output
true true true
Example 2
Let’s examine the isInteger function through a series of test scenarios.
Example
<script>
function check(x,y)
{
return x/y;
}
document.writeln(Number.isInteger(check(12,2)));
document.writeln(Number.isInteger(check(12,5)));
</script>
Output:
Output
true false
Example 3
Let's examine a few values for which the isInteger method yields a false result.
Example
<script>
document.writeln(Number.isInteger(2.5));
document.writeln(Number.isInteger(-2.5));
document.writeln(Number.isInteger('2.5'));
document.writeln(Number.isInteger(Infinity));
document.writeln(Number.isInteger(-Infinity));
document.writeln(Number.isInteger(NaN));
</script>
Output:
Output
false false false false false false