Check if a value is a number in JavaScript
4 min read
JavaScript provides us with a few ways to check if a value is a number. We'll look over three of those ways and show the best way to check for a numeric value in JavaScript.
isNaN()
The isNaN()
function checks if the passed value is not a number or can't be converted into a Number. If isNaN()
returns false
, the value is a number and can't be converted into a Number. Let's see a few examples.
const value = 100
isNaN(value) // false
isNaN(5.0) // false
isNaN('test') // true
isNaN([]) // false
isNaN({}) // true
isNaN(NaN) // true
isNaN(Number.NaN) // true
isNaN(0/0) // true
isNaN
also returns true
for cases where the value is not actually the value NaN
. For example, the string 'test'
, is not a number and cannot be converted into a number, but it is not NaN
. Yet, isNaN('test')
returns true
.
When isNaN()
receives anything but a number, it coerces the value to a number. As a result, isNaN()
can produce unexpected results. It's better to use Number.isNaN()
instead of isNaN()
to avoid bugs.
Number.isNaN()
The Number.isNaN()
method is a more reliable way to check if a value is a number. Number.isNaN()
will only check if the value passed to it is NaN
and its type is Number.
Let's compare Number.isNaN()
with isNaN()
.
Number.isNaN("foo") // false
isNaN("foo") // true
Remember that isNaN()
tries to coerce "foo"
into a number and can give surprising results. In this case, it gives us the wrong result, telling us that the string "foo"
is the value NaN
, but it's actually not.
Number.isNaN()
gives us the correct answer. The string "foo"
is not the value NaN
and is not of type number, so it returns false
. This simple example shows that Number.isNaN()
is the more robust method of the two, and it should be used instead of isNaN()
.
Let's see a few examples of Number.isNaN()
.
const value = 100
Number.isNaN(value) // false
Number.isNaN(5.0) // false
Number.isNaN('test') // false
Number.isNaN([]) // false
Number.isNaN({}) // false
Number.isNaN(NaN) // true
Number.isNaN(Number.NaN) // true
Number.isNaN(0/0) // true
The values NaN
, Number.NaN
and 0/0
return true
while all others return false
. Division by zero results in NaN
.
typeof
Another way to check if a value is a number in JavaScript is to use the typeof
operator. When used on a numeric value, it will return a "number"
string.
typeof 100 // "number"
typeof NaN // "number"
typeof {} // "object"
typeof [] // "object"
typeof null // "object"
Notice that the value
NaN
, meaning Not-A-Number, is actually of type number.
We can write a conditional statement to check if a value is a number using the typeof
operator.
const value = 100;
if (typeof value === 'number') {
console.log('A number');
} else {
console.log('NOT a number');
}
Notice above that typeof NaN
is actually of type number. This means that using typeof value === 'number'
in a conditional expression can lead to misleading results. If value
is set to NaN
, the conditional check would pass as true
. It would be a false positive.
We probably don't want something that is actually Not-A-Number (NaN
) to make a numeric check pass. Therefore, we should combine the Number.isNaN()
method with the typeof
operator to make sure that NaN
values don't end up causing unexpected results.
const value = 100;
if (typeof value === 'number' && !Number.isNaN(value)) {
console.log('A number');
} else {
console.log('NOT a number');
}
The !
operator (the logical NOT operator) was used on the Number.isNaN()
method because we only want to enter the if
statement when the value
is NOT NaN
.
Conclusion
- Avoid
isNaN()
for checking if a value is a number. - Use
Number.isNaN()
instead ofisNaN()
. - If you are using the
typeof
operator to check if a value is a number, combine thetypeof
operator withNumber.isNaN()
for best results.