LOGBOOK

HELP

Quiz Entry - updated: 2026.07.05

What is NaN in JavaScript, and how do you correctly check for it?

NaN ("Not a Number") marks a failed numeric operation, and the catch is that it isn't even equal to itself — so you must check it with Number.isNaN(), not ===.

NaN is the special value JavaScript produces when a calculation that should yield a number can't:

let result = 'hello' - 5;   // NaN — 'hello' can't become a number
let invalid = parseInt('abc');  // NaN — nothing numeric to parse

Now the genuinely surprising part: NaN is not equal to anything, including itself. This means the obvious checks silently fail:

result == NaN     // false — always, even though result IS NaN
result === NaN    // false — always

So you can never test for NaN with == or ===. Instead use a dedicated function:

isNaN(result)         // true — but it coerces its argument first
Number.isNaN(result)  // true — stricter and safer

Prefer Number.isNaN(): the older global isNaN() first coerces its argument, so isNaN('hello') is misleadingly true even though 'hello' isn't NaN. Number.isNaN() returns true only for an actual NaN, which is almost always what you mean.

Go deeper:

  • doc MDN: NaN — why NaN !== NaN (IEEE 754), and the safe ways to test for it including the v !== v trick.

From Quiz: WEBT / Introduction to JavaScript | Updated: Jul 05, 2026