Check if variable is a number in JavaScript
In JavaScript, there are two ways to check if a variable is a number :
- isNaN() – Stands for “is Not a Number”, if variable is not a number, it return true, else return false.
- 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.
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 :
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 :
Download Source Code
Download it – JavaScript-Check-If-Variable-Is-Number.zip (1 KB)
isNaN is not reliable, try it with an empty string.
function isNumber(num){
return !isNaN(parseFloat(num)) && isFinite(num);
}
isNaN is not for checking numbers. It can only check if a variable is ‘NaN’ type.
isNaN doesn’t work for booleans: isNaN(true) = false
I think under the hood true and false are numbers, at least in bits. True = 11111111 and false = 00000000. Maybe that’s why?
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
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.
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”);
Hi mkyong,
Please specify the complete use case of typeof, as it will not work if input is user defined.
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
your functions is not valid for 55.23
isNaN is unreliable
MDN states : “this function is broken”
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN#Description
isNaN(” “) // returns false
isNaN(“x”) // returns true
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.
I dont’t see how this is logical any way. no good didn’t help
It would be nice if your code was formatted nicely.
This is so inaccurate.
This will throw ReferenceError so is not useful by itself if value could be undefined:
This will alert the browser so typeof should not be used like you say.
Js sucks completely. I.e isNaN(“111”) => false or let nan = NaN; typeof nan => ‘number’ This language is a madness!
yep, JS has many unlogical decisions (most of time fataly unlogical)
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
This will fail if you give a parameter like ‘ ‘ or ” “. These are not numbers just an empty space
if ($.isNumeric($(“.inputText”).val()))
alert(“Yes”);
else
alert(“No”);
What about the string “100” is that a number or a string?