Question presented to candidate: "isNaN('hello') returns true. Does that mean the string 'hello' actually IS NaN? What's really happening there, and what should you use instead?"
What a strong answer should cover:
- 📌 Interview term:
NaN === NaNisfalse— the famous, real starting trap:NaNis the one value in JavaScript that is genuinely never equal to itself under===, so equality comparison alone can never detect it. - 📌 Interview term: the global
isNaN()— verified directly: it genuinely coerces its argument to a number FIRST, then checks if the coerced result isNaN— this is exactly whyisNaN("hello")istrue:"hello"isn't literally the valueNaN, it just genuinely FAILS numeric coercion, producingNaN, which the global function then reports. - 📌 Interview term: the real, direct answer to the prompt — verified directly:
"hello"is genuinely notNaNitself;isNaN("hello")beingtrueis a real, misleading side effect of coercion, not a report that the string literally holds the valueNaN.Number.isNaN("hello")— the correct, precise check — genuinely does NOT coerce at all, correctly returningfalse. - 📌 Interview term:
Number.isNaN()— verified directly: it only returnstruefor the value that is LITERALLYNaN(including the real result of0/0) — no coercion, no false positives for non-numeric strings. - A precise answer names
Object.is(x, NaN)as a real, valid alternative producing the identical correct result toNumber.isNaN(x), sinceObject.is(unlike===) is specifically defined to treatNaNas equal to itself.
Clarifying questions expected:
- None — this is a definitional/technical question; directly answering WHY
isNaN("hello")is misleadinglytrue(coercion, not literal NaN) is the strong signal.
Code / implementation expected: Yes — the direct side-by-side isNaN() vs. Number.isNaN() comparison on the exact same inputs is the clearest, most convincing demonstration.