Check if variable is a number in JavaScript

In JavaScript, there are two ways to check if a variable is a number :

  1. isNaN() – Stands for “is Not a Number”, if variable is not a number, it return true, else return false.
  2. typeof – If variable is a number, it will returns a string named “number”.
Note
Normally, people use isNaN() to check number, but typeof is a nice try also.

1. isNaN() Example

Example to use JavaScript’s isNaN() function.


<html>
<head></head>
<body>
<h1>isNaN() example</h1>

<script type="text/javascript">
 var num1 = 100;
 if(isNaN(num1)){
	document.write(num1 + " is not a number <br/>");
 }else{
	document.write(num1 + " is a number <br/>");
 }
 
 var str1 = "mkyong"
 if(isNaN(str1)){
	document.write(str1 + " is not a number <br/>");
 }else{
	document.write(str1 + " is a number <br/>");
 }
</script>

</body>
</html>

See result :

isNaN example

2. typeof Example

Example to use JavaScript’s “typeof” operator.


<html>
<body>
<h1>JavaScript : typeof example</h1>

<script type="text/javascript">
 var num1 = 100;

 if(typeof num1 == 'number'){
	document.write(num1 + " is a number <br/>");
 }else{
	document.write(num1 + " is not a number <br/>");
 }
</script>

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

See result :

typeof example

Download Source Code

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
13 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
16 Comment authors
Pablo PazosAbhishek SrivastavaAAAquestTomasz Sz Recent comment authors
newest oldest most voted
Howard Cary Morris
Guest
Howard Cary Morris

To check if variable A is an integer may use (parseIint(A)==A)
You might want to strip out currency symbols and commas (US, UK) first

PHPSoft
Guest
PHPSoft

Nice examples. But what about numbers like 99,99 or 99.99 it’s also valid numbers but your script will fail to recognize some of them.

Pablo Pazos
Guest
Pablo Pazos

isNaN is not reliable, try it with an empty string.

Abhishek Srivastava
Guest
Abhishek Srivastava

Hi mkyong,
Please specify the complete use case of typeof, as it will not work if input is user defined.

AAA
Guest
AAA

isNaN is not for checking numbers. It can only check if a variable is ‘NaN’ type.

Otman Bouchari
Guest
Otman Bouchari

function isNumber(num){
return !isNaN(parseFloat(num)) && isFinite(num);
}

Danny Umansky
Guest
Danny Umansky

isNaN doesn’t work for booleans: isNaN(true) = false

Sunil Williams
Guest
Sunil Williams
Per Bothner
Guest
Per Bothner

The function isNaN is not necessarily broken – – but the suggestion to use it to check “variable is not a number” is confused. First, it checks values, not variables. More fundamentlly, it completely misunderstand what teh function does.

Tomasz Sz
Guest
Tomasz Sz

isNaN(” “) // returns false
isNaN(“x”) // returns true

P
Guest
P

What about the string “100” is that a number or a string?

RunTimeException
Guest
RunTimeException

This will fail if you give a parameter like ‘ ‘ or ” “. These are not numbers just an empty space

amin
Guest
amin

if ($.isNumeric($(“.inputText”).val()))
alert(“Yes”);
else
alert(“No”);

Marek Marczak
Guest
Marek Marczak

Js sucks completely. I.e isNaN(“111”) => false or let nan = NaN; typeof nan => ‘number’ This language is a madness!

quest
Guest
quest

yep, JS has many unlogical decisions (most of time fataly unlogical)

Arunsanjeev
Guest
Arunsanjeev

Join me 9788641665