Must Know About isNaN (Not a Number Conditional)

Have been going through the JavaScript introduction tutorial within the Mozilla Developer Network.

And have found the following snippet as very salient when working with numbers in JavaScript — which is almost on a daily basis!

This relates to the Not-a-number function.

The following is the wrong way to use the isNaN() wherein the Number prototype is not specified:

Number.isNaN(NaN); // true
Number.isNaN('hello'); // false
Number.isNaN('1'); // false
Number.isNaN(undefined); // false
Number.isNaN({}); // false
Number.isNaN([1]) // false
Number.isNaN([1,2]) // false

☑️ The correct way to use isNaN()

When working with isNaN() we must always use the Number prototype to call it:

isNaN('hello'); // true
isNaN('1'); // false
isNaN(undefined); // true
isNaN({}); // true
isNaN([1]) // false
isNaN([1,2]) // true

Conclusion

This gem of an answer below, clearly states this, and this blog article can act as a reference point for the future when working with isNaN():Due to both equality operators, == and ===, evaluating to false when checking if NaN is NaN, the function Number.isNaN() has become necessary.In comparison to the global isNaN() function, Number.isNaN() doesn’t suffer the problem of forcefully converting the parameter to a number.This means it is now safe to pass values that would normally convert to NaN, but aren’t actually the same value as NaN.This also means that only values of the type number, that are also NaN, return true.
...