Check if Number Is Integer JavaScript|Number.isInteger

Check if Number Is Integer JavaScript|Number.isInteger

Overview

When you work with javascript. Most times you check that value is string, number, integer number, decimal number, etc. So In this tutorial, you will learn how you can check value is a number or not in javascript using the number.isInteger() method.

In this tutorial explain, how you can check if a number is an integer value or not in javascript.

You can easily check if a number is an integer or not in javascript using the number.isInteger() function.

Number.isInteger() Method

In JavaScript Number.isInteger() method works with numbers, it will check whether a value an integer. This method returns true if the value is of the type Number and an integer. Otherwise, it will return false.

If the target value is an integer, return true, otherwise, return false. If the value is NaN or Infinity, return false. The method will also return true for floating-point numbers that can be represented as an integer.

Syntax

The syntax of number.isInterger() method is:

Number.isInteger(value)

Parameters

This method accept only one parameter.

Example of the Number.isInterger()

You can use this below source code to check a given number is an integer or not in javascript.

We will use javascript number method, The Number() the method converts a string to a number.

<!DOCTYPE html>
<html>
<title>Check if Number Is Integer JavaScript | Number.isInteger</title>
<head></head>
<body>
<h2>How to Check if Number Is Integer or Not In JavaScript</h2>
<br/>
<input type="text" id="is_interger" value="" placeholder="Please enter here.....">
<br/><br/>
<button onclick="checkIntergerJavaScript()">Click &amp; See Result</button>
<br/>
<script type="text/javascript">
    function checkIntergerJavaScript() {

      var num = document.getElementById("is_interger").value; 
      if(Number.isInteger(Number(num))){
        alert(num + " is a integer number");
       }else{
        alert(num + " is not an integer number");
       }
  }

</script>

</body>
</html>

Demo

Check if Number Is Integer JavaScript | Number.isInteger
How to Check if Number Is Integer or Not In JavaScript




Conclusion

In this javascript tutorial of the Number.isInteger() method, you have learned how you can check a number is an integer or not in javascript using the is Number.isInteger() method. In javaScript Number.isInteger() function to check whether the value passed to it is an integer or not. It returns true if the passed value is an integer, otherwise, it will return false.

If you have any questions or thoughts to share, use the comment form below to reach us.

Leave a Reply

Your email address will not be published. Required fields are marked *