To tell if a number in JavaScript is an integer, we can use the JavaScript Number.isInteger() method.

Number.isInteger(3)

The above code would return true, since 3 in an integer.

Some other examples of JavaScript isInteger method are below:

var result1 = Number.isInteger(1);
var result2 = Number.isInteger(0);
var result3 = Number.isInteger(-10);
var result4 = Number.isInteger(1.5);
var result5 = Number.isInteger(100.00);
var result6 = Number.isInteger("10");

Which would result in the following:

result1 = true;
result2 = true;
result3 = true;
result4 = false;
result5 = true;
result6 = false;

JavaScript isInteger in action using jQuery

Below we will provide code to let the user input a number, and then use the Number.isInteger() method to find out if the number is an integer or not. Here is our simple HTML setup:

Type a number you want to use the Number.isInteger() method on below:




Below is the JavaScript and jQuery code which take the user input using the jQuery click() or on() keypress methods, and use the Number.isInteger() method on that user input and update the results below using the jQuery text() method.

$('#userVal').on('keypress',function(e) {
  if(e.which == 13) {
    $("#results").text(Number.isInteger(Number$("#userVal").val())));
  }
});
$("#submitNum").click(function(){
  $("#results").text(Number.isInteger(Number$("#userVal").val())));
});

The final code and output for this example is below:

Code Output:

Type a number you want to use the Number.isInteger() method on below:


Full Code:

Type a number you want to use the Number.isInteger() method on below:





<script>

$('#userVal').on('keypress',function(e) {
  if(e.which == 13) {
    $("#results").text(Number.isInteger(Number($("#userVal").val())));
  }
});
$("#submitNum").click(function(){
  $("#results").text(Number.isInteger(Number$("#userVal").val())));
});

</script>

Hopefully this article has been useful in helping you to understand how to check if a number is an integer using the JavaScript isInteger method.

Click here to learn more.

Categorized in:

JavaScript,

Last Update: May 3, 2024