Confusion between isNaN and Number.isNaN in javascript

To quote from a ponyfoo article on numbers in ES6:

Number.isNaN is almost identical to ES5 global isNaN method.
Number.isNaN returns whether the provided value equals NaN. This is a
very different question from “is this not a number?”.

So isNaN just checks whether the passed value is not a number or cannot be converted into a Number. Number.isNaN on the other hand only checks if the value is equal to NaN (it uses a different algorithm than === though).

The String 'ponyfoo' for example is not a number and cannot be converted into a number, but it is not NaN.

Example:

Number.isNaN({});
// <- false, {} is not NaN
Number.isNaN('ponyfoo')
// <- false, 'ponyfoo' is not NaN
Number.isNaN(NaN)
// <- true, NaN is NaN
Number.isNaN('pony"https://stackoverflow.com/"foo')
// <- true, 'pony"https://stackoverflow.com/"foo' is NaN, NaN is NaN

isNaN({});
// <- true, {} is not a number
isNaN('ponyfoo')
// <- true, 'ponyfoo' is not a number
isNaN(NaN)
// <- true, NaN is not a number
isNaN('pony"https://stackoverflow.com/"foo')
// <- true, 'pony"https://stackoverflow.com/"foo' is NaN, NaN is not a number

Leave a Comment