JavaScript Number isInteger() method - JavaScript Tutorial

JavaScript Number isInteger() method

BLUF: This tutorial on JavaScript Number isInteger() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript Number isInteger() method

Understanding JavaScript Number isInteger() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

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

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience