Number.isNaN vs Global isNaN
You'll learn why Number.isNaN is more reliable than the global isNaN function, and when each one should be used.
The Problem with Global isNaN
JavaScript has two ways to check if a value is NaN: the global isNaN() and Number.isNaN(). They behave very differently.
The global isNaN() attempts to convert its argument to a number first, then checks if it's NaN. This means it returns true for many non-numeric values that aren't actually NaN:
isNaN("hello"); // true (coerces "hello" to NaN)
isNaN(undefined); // true (coerces undefined to NaN)
isNaN({}); // true (coerces object to NaN)
isNaN(NaN); // true (actually is NaN)
Think of global isNaN as asking "would this become NaN if I tried to make it a number?" — not "is this literally NaN?"
Number.isNaN: The Precise Check
Number.isNaN() performs a strict check without any type conversion. It only returns true if the value is actually NaN:
Number.isNaN("hello"); // false (it's a string, not NaN)
Number.isNaN(undefined); // false (it's undefined, not NaN)
Number.isNaN({}); // false (it's an object, not NaN)
Number.isNaN(NaN); // true (this IS NaN)
When to Use Each
Use Number.isNaN() when you want to check if a value is specifically the NaN value. Use global isNaN() only if you genuinely want to test whether something would fail to be a valid number after coercion.
Key Takeaway: Number.isNaN() is the reliable, strict way to check for NaN without unexpected type coercion — prefer it over global isNaN() in modern JavaScript.