How To Check if Number Is Integer JavaScript

How To Check if Number Is Integer JavaScript


Number Is Integer JavaScript

This article is about. How To Check if Number Is Integer JavaScript.

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.

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

Numbe is integer() 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.isInteger()

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!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>

 

Leave a Reply

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