How to Check if Number Is Integer or Float in JavaScript

To check if number is integer or float in javascript; Through this tutorial, i am going to show you how to check if number is integer or float in JavaScript.

How to Check if Number Is Integer or Float in JavaScript

There are two ways to check if number or variable is integer or float in JavaScript; as shown below:

  • To Check if Number Is Integer in JavaScript
  • To Check if Number Is Float or Integer in JavaScript

To Check if Number Is Integer in JavaScript

See the following example for how to check if number is integer in javascript; as shown below:

<!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 & 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>

To Check if Number Is Float or Integer in JavaScript

See the following example for how to check if number is float or integer in javascript; as shown below:

<!DOCTYPE html>
<html>
<title>How to Check if Number Is Integer OR Float In JavaScript</title>
<head></head>
<body>
<h2>How to Check if Number Is Integer OR Float In JavaScript</h2>
<br/>
<input type="text" id="is_val" value="" placeholder="Please enter here.....">
<br/><br/>
<button onclick="checkNumber()">Click & See Result</button>
<br/>
<script type="text/javascript">

function checkNumber(x) {

     var num = document.getElementById("is_val").value;
     var x = Number(num); 
    // check if the passed value is a number
    if(typeof x == 'number' && !isNaN(x)){
    
        // check if it is integer
        if (Number.isInteger(x)) {
            alert(`${x} is integer.`);
        }
        else {
            alert(`${x} is a float value.`);
        }
    
    } else {
        alert(`${x} is not a number`);
    }
}

</script>
</body>
</html>

Conclusion

To check if number is integer or float in javascript; Through this tutorial, we have learned how to check if number is integer or float in JavaScript.

Recommended JavaScript Tutorials

Recommended:-JavaScript Arrays

Be First to Comment

Leave a Reply

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