NaN matters in production numeric validation because it is the only JavaScript value not equal to itself, global isNaN coerces inputs, and empty strings or malformed payloads create common mistake cases.
Use this JavaScript interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.
What is the NaN property in JavaScript?Frontend interview answer
This JavaScript interview question tests whether you can explain JavaScript NaN common mistakes: Number.isNaN, coercion traps, and validation bugs, connect it to production trade-offs, and handle common follow-up questions.
- JavaScript NaN common mistakes: Number.isNaN, coercion traps, and validation bugs explanation without falling back to memorized docs wording
- Global Object and Isnan reasoning, edge cases, and production failure modes
- How you would answer the most likely JavaScript interview follow-up
Common mistake
NaN is not just a trivia definition. It is a production validation pitfall because NaN !== NaN, global isNaN coerces values, and empty strings or bad payloads can silently turn checks into bugs. The safer debugging rule is usually Number.isNaN.
console.log(0 / 0); // NaN
console.log(parseInt('Hi')); // NaN
console.log(Math.sqrt(-1)); // NaN
Property | Description | Example |
|---|---|---|
Type | The type of |
|
Equality |
|
|
Check | Use |
|
Why not just 'isNaN'?
The global isNaN() function tries to coerce values before checking, which can give confusing results. Always prefer Number.isNaN() because it doesn’t perform type coercion.
isNaN('Hello'); // true ❌ (string coerced)
Number.isNaN('Hello'); // false ✅
Practical scenario
A numeric input can return '' or 'foo'; you need to detect NaN before calculations.
Common pitfalls
- Using global
isNaN, which coerces values. - Assuming
NaN === NaNis true. - Mixing
parseIntandNumberwithout checks.
Prefer
Number.isNaN for accuracy. Test empty strings, null, and non-numeric inputs.Think of NaN like a 'broken calculator' output — the operation ran, but the result doesn’t make mathematical sense, so JavaScript gives you NaN instead of crashing.
NaNrepresents 'Not-a-Number' but its type is actuallynumber.
- It results from invalid numeric operations.
- It’s the only value in JS that is not equal to itself.
- Use
Number.isNaN()instead ofisNaN()to check safely.
Use this as one explanation rep, then continue with the JavaScript interview questions cluster or a guided prep path.