Course contentsShow
← JavaScript
Lesson 123 of 2,7234. Working with NumbersFree lesson

Detecting NaN with Number.isNaN

Use Number.isNaN to reliably check for NaN without type coercion surprises.

Detecting NaN with Number.isNaN

You'll learn how to reliably check if a value is actually NaN without unexpected surprises.

The Problem with NaN

You already know that NaN is JavaScript's special value for "not a number" — but here's the tricky part: checking for it is weird. You can't use === because NaN is the only value in JavaScript that doesn't equal itself:

NaN === NaN  // false (surprising!)

Why Number.isNaN Exists

JavaScript gives us Number.isNaN() as the reliable, no-surprises way to detect NaN. It checks two things:

  1. Is the value actually the Number type?
  2. Is it specifically the NaN value?
Number.isNaN(NaN)           // true
Number.isNaN(0 / 0)         // true (creates NaN)
Number.isNaN(5)             // false
Number.isNaN("hello")       // false (it's a string, not NaN)
Number.isNaN(undefined)     // false

The Key Difference

Number.isNaN() does not perform type coercion. It won't try to convert your value to a number first — it checks what you actually give it, right now.

Think of it like a strict ID check: "Show me your actual ID, not what you might convert into."

Number.isNaN("not a number")  // false (it's a string)
Number.isNaN(undefined)       // false (it's undefined)

Use Number.isNaN() whenever you need to check if a calculation produced NaN, especially after operations that might fail (like parsing or math with invalid inputs).

Key Takeaway: Use Number.isNaN() to check for NaN reliably — it returns true only for the actual NaN value, with no type conversion tricks.