Example 1 — Check Different Values
Pass numbers, text and undefined to isNaN().
console.log("NaN: " + isNaN(NaN));
console.log("42: " + isNaN(42));
console.log("'42': " + isNaN("42"));
console.log("'3.5': " + isNaN("3.5"));
console.log("'hello': " + isNaN("hello"));
console.log("'12px': " + isNaN("12px"));
console.log("undefined: " + isNaN(undefined));
console.log("0 / 0: " + isNaN(0 / 0));
console.log("NaN === NaN: " + (NaN === NaN)); How It Works
- Numbers and numeric text.
42and"42"are both fine, soisNaN()returnsfalse. - Text that is not a number.
"hello"and"12px"cannot be converted, so they becomeNaNand the answer istrue. Note that"12px"fails here even thoughparseInt("12px", 10)would read12. - Maths that fails.
0 / 0has no answer, so JavaScript givesNaN. - Why you need isNaN(). The last line shows that
NaN === NaNisfalse, so you cannot test forNaNwith===.

