NaN stands for Not-a-Number and is the only value in JavaScript that is not equal to itself. Use Number.isNaN to check for it reliably, because global isNaN coerces values. This is important for numeric validation and edge cases. Edge cases include empty strings and non-numeric input; test with Number.isNaN.
What is the NaN property in JavaScript?
The Core Idea
NaN stands for Not-a-Number. It is a special numeric value that represents the result of an invalid mathematical operation — something that cannot produce a meaningful number.
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 the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.