Borislav Hadzhiev
Thu Oct 14 2021·2 min read
Photo by Paola Chaaya
To check if a number is NaN
, call the Number.isNaN()
method, passing it
the number as a parameter. The Number.isNaN
method returns true
if the
passed in value is NaN
and has a type of number
, otherwise it returns
false
.
const num = 0 / 'test'; console.log(num); // 👉️ NaN if (Number.isNaN(num)) { // 👉️ this runs only if NaN and type of number console.log('Number is NaN'); }
We use the
Number.isNaN
method to check if a value has a type of number
and is NaN
.
You should never try the equality operators when checking for NaN
because as
confusing as it may seem, NaN
is not equal to NaN
in Javascript.
// 👇️ don't do this console.log(Number.NaN === Number.NaN); // 👉️ false
NaN
is the only value in JavaScript that is not equal to itself.The Number.isNaN
only returns true
if the following conditions are met:
number
NaN
All the following examples return false
.
console.log(Number.isNaN('string')); // 👉️ false console.log(Number.isNaN(undefined)); // 👉️ false console.log(Number.isNaN(null)); // 👉️ false console.log(Number.isNaN([])); // 👉️ false console.log(Number.isNaN({})); // 👉️ false
Number.isNaN
method is not supported in Internet Explorer version 6-11. If you have to support the browser, use the polyfill from the next section.If you need to support Internet Explorer add the following polyfill
to your
script, before you try to use the Number.isNaN
method.
Number.isNaN = Number.isNaN || function isNaN(input) { return typeof input === 'number' && input !== input; }
This code is quite intuitive, we first check if the Number.isNaN
method is
supported by the browser.
If it isn't supported by the browser we implement it by attaching a function to
the isNaN
property of the Number
object.
Our function checks if the type of the provided value is of type number
and
it's not equal to itself.
Since NaN
is the only value in JavaScript that is not equal to itself this is
sufficient to determine whether the user input is NaN
.
Hopefully they don't come up with other values that are not equal to themselves, that would break our polyfill.
An alternative approach is to use the older isNaN method, which is supported by Internet Explorer.
isNaN
method is very unintuitive.If the argument provided to the isNaN
method is not of type number
, the
method coerces the value to a number
, before checking if it's NaN
.
This is different from the Number.isNaN
method, which doesn't coerce the
passed in value.
Here are some examples of using the strange isNaN
method:
console.log(isNaN(0 / 0)); // 👉️ true console.log(isNaN('string')); // 👉️ true console.log(isNaN(undefined)); // 👉️ true console.log(isNaN({})); // 👉️ true console.log(isNaN(null)); // 👉️ false console.log(isNaN([])); // 👉️ false
Had we used the newer Number.isNaN
method with these values, the only true
response would come from calling the method with 0 / 0
.
It is generally advised against using methods that do more than they need to and might surprise you and cause difficult to track bugs in your application.
Number.isNaN
method instead and add the polyfill if you have to support Internet Explorer.