Skip to content

JavaScript IsNumeric function like jQuery?

  • by

How to write isNumber() in JavaScript?

First, there is no function like isNumeric in pure JavaScript. But jQuery has this function to check the integers.

but you could add your own function like below example:

function isNumeric(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

Complete Example of JavaScript IsNumeric

HTML example code:-

<!DOCTYPE html> 
<html>
  
<body> 
    <script type="text/javascript"> 

    function isNumeric(n) {
  		return !isNaN(parseFloat(n)) && isFinite(n);
	}     

	var num = 10;
	var check = isNumeric(num)
	alert(check)

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

Output:

Example of JavaScript IsNumeric

NOTE: Since parseInt() is not a proper way to check for numeric it should NOT be used.

Q: How to Check if variable is a number in JavaScript?

Answer:  You can use isinteger JavaScript Method:

<!DOCTYPE html>
<html>
  <head>
    <script>
 
        var a = Number.isInteger(-7);  
 	var b = Number.isInteger(-1.96);
        alert(a)
 
    </script>
  </head>   
 
</html>

Read more:

Do comment if you have any question 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

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading