Main Tutorials

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 Author

author image
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

Subscribe
Notify of
25 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Pablo Pazos
4 years ago

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

Otman Bouchari
5 years ago

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

AAA
4 years ago

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

Danny Umansky
5 years ago

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

André
1 year ago
Reply to  Danny Umansky

I think under the hood true and false are numbers, at least in bits. True = 11111111 and false = 00000000. Maybe that’s why?

John
1 year ago
Reply to  André

Booleans only use 1 bit not 8, so it should be true = 1, false = 0.
Also that argumentation doesn’t make sense, because everything is represented internally as bits, even NaN

PHPSoft
10 years ago

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.

Mohssine EL HARFI
3 years ago

using regex maybe ?

var value = “123.55”;
var reg = /^\d*\.?\d+$/;
if(reg.test(value))
  console.log(value + ” is an integer”);
else
console.log(value + ” is NaN”);

Abhishek Srivastava
4 years ago

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

alex
2 years ago

Funny as it sounds, NaN is a number. If you try typeof NaN returns Number.
So the correct solution would be something like this:

function isValidNumber (num) {
return typeof num === ‘number’ && !isNaN(num);
}

isValidNumber(4) // true
isValidNumber(“4”) // false
isValidNumber(NaN) // false

john
3 years ago

your functions is not valid for 55.23

Sunil Williams
9 years ago
Tomasz Sz
5 years ago
Reply to  Sunil Williams

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

Per Bothner
6 years ago
Reply to  Sunil Williams

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.

biden
1 year ago

I dont’t see how this is logical any way. no good didn’t help

Emina
1 year ago

It would be nice if your code was formatted nicely.

Daniel
3 years ago

This is so inaccurate.

This will throw ReferenceError so is not useful by itself if value could be undefined:

// supposing var undefinedValue is undefined
if (isNaN(undefinedValue) )
  alert("You would want probably a condition to catch this without throwing error");

This will alert the browser so typeof should not be used like you say.

var nanValue = NaN;
if (typeof nanValue == "number)
  alert("Not a number has type number");
Marek Marczak
5 years ago

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

quest
4 years ago
Reply to  Marek Marczak

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

Howard Cary Morris
6 years ago

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

RunTimeException
10 years ago

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

amin
10 years ago

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

P
9 years ago

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