Skip to content

Number isinteger JavaScript Method | check var/value is an integer or not

  • by

Number isinteger JavaScript Method is used to know whether a value an integer or not? You have to pass the value into an isinteger() function and its return boolean value, true if the value is of the type Number. Otherwise, it returns false.

Syntax

Number.isInteger(value)

Parameter Values

A single parameter value value to be tested.

Return Value

It will return true if the type Number and an integer, else it returns false.

Example of JavaScript number isinteger

Let’s see the examples with different types of values.

Negative number

<!DOCTYPE html>
<html>
  <head>
    <script>

        var a = Number.isInteger(-7);  
 		var b = Number.isInteger(-1.96);
        alert(a)

    </script>
  </head>   

</html>

Output:

Number isinteger JavaScript Method

Positive number

<!DOCTYPE html>
<html>
  <head>
    <script>

    	var n = 9
        var a = Number.isInteger(n);  
        alert(a)

    </script>
  </head>   

</html>

Q: How JavaScript check if the string contains only numbers?

Answer: Use regular expressions to check string contains only numbers in JS.

<!DOCTYPE html>
<html>
  <head>
    <script>

    	var val = "9"
        var isnum = /^\d+$/.test(val)
        alert(isnum)

    </script>
  </head>   

</html>

JavaScript number validation

RobG wisely suggests the more succinct /\D/.test(z). This operation tests the inverse of what you want. It returns true if the input has any non-numeric characters

Q: How to JavaScript check if the string is an integer?

Answer: To check if a variable (including a string) is a number, check if it is not a number:

isNaN(num)         // returns true if the variable does NOT contain a valid number

Examples

isNaN(123)         // false
isNaN('123')       // false
isNaN('1e10000')   // false (This translates to Infinity, which is a number)
isNaN('foo')       // true
isNaN('10px')      // true

Do comment if you have any doubts and suggestions on this tutorial.

Note: The All JS Examples codes are tested on the Safari browser (Version 12.0.2) and Chrome.
OS: macOS 10.14 Mojave
Code: HTML 5 Version

Leave a Reply

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