JavaScript Number.isInteger() | Check if value is an integer

The JavaScript Number.isInteger() method is used when we need to check if a variable or a value is an integer of datatype Number. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <p id="xyz"></p>

   <script>
      let num = 10;
      document.getElementById("xyz").innerHTML = Number.isInteger(num);
   </script>
   
</body>
</html>
Output

That is, the Number.isInteger() returns true if specified value is an integer. Otherwise returns false.

JavaScript Number.isInteger() Syntax

The syntax of Number.isInteger() method in JavaScript is:

Number.isInteger(x)

The x parameter is required, refers to a value that needs to be checked whether it is an integer or not.

JavaScript Number.isInteger() Example

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <p>1. <span id="span1"></span></p>
   <p>2. <span id="span2"></span></p>
   <p>3. <span id="span3"></span></p>
   <p>4. <span id="span4"></span></p>
   <p>5. <span id="span5"></span></p>
   <p>6. <span id="span6"></span></p>
   <p>7. <span id="span7"></span></p>
   <p>8. <span id="span8"></span></p>
   <p>9. <span id="span9"></span></p>
   <p>10. <span id="span10"></span></p>
   <p>11. <span id="span11"></span></p>

   <script>
      document.getElementById("span1").innerHTML = Number.isInteger(0);
      document.getElementById("span2").innerHTML = Number.isInteger(0/0);
      document.getElementById("span3").innerHTML = Number.isInteger(1.5);
      document.getElementById("span4").innerHTML = Number.isInteger(-20);
      document.getElementById("span5").innerHTML = Number.isInteger(false);
      document.getElementById("span6").innerHTML = Number.isInteger(true);
      document.getElementById("span7").innerHTML = Number.isInteger(Infinity);
      document.getElementById("span8").innerHTML = Number.isInteger(NaN);
      document.getElementById("span9").innerHTML = Number.isInteger(10-32);
      document.getElementById("span10").innerHTML = Number.isInteger(32/54);
      document.getElementById("span11").innerHTML = Number.isInteger('30');
   </script>
   
</body>
</html>
Output

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

Check if a value is an integer in JavaScript

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <p id="xyz"></p>

   <script>
      let num = 10;
      if(Number.isInteger(num))
         document.getElementById("xyz").innerHTML = num + " is an integer."
      else
         document.getElementById("xyz").innerHTML = num + " is not an integer."
   </script>
   
</body>
</html>
Output

JavaScript Online Test


« Previous Tutorial Next Tutorial »