What is the NaN property in JavaScript?

MediumEasyJavascript
Quick Answer

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.

Answer

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.

JAVASCRIPT
console.log(0 / 0);         // NaN
console.log(parseInt('Hi'));  // NaN
console.log(Math.sqrt(-1));   // NaN
                  

Property

Description

Example

Type

The type of NaN is actually number.

typeof NaN // 'number'

Equality

NaN is the only value in JavaScript that is not equal to itself.

NaN === NaN // false

Check

Use Number.isNaN() to check if a value is truly NaN.

Number.isNaN(NaN) // true

Key characteristics of NaN in JavaScript.

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.

JAVASCRIPT
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 === NaN is true.
      • Mixing parseInt and Number without checks.
Trade-off or test tip
Prefer Number.isNaN for accuracy. Test empty strings, null, and non-numeric inputs.

Still so complicated?

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.

Summary
  • NaN represents 'Not-a-Number' but its type is actually number.
  • It results from invalid numeric operations.
  • It’s the only value in JS that is not equal to itself.
  • Use Number.isNaN() instead of isNaN() to check safely.
Similar questions
Guides
Preparing for interviews?

Use the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.