Computer >> Computer tutorials >  >> Programming >> Javascript

How to check if a variable is an integer in JavaScript?


To check if a variable is an integer, check the value using parseInt() and check with === operator.

Example

You can try to run the following code to check the existence of an integer −

<html>
   <head>
      <script>
         var a = 10;
         if (a === parseInt(a, 10))
            alert("Integer!")
         else
           alert("Not an integer!")
      </script>
   </head>
   <body>
   </body>
</html>