# null, undefined & NaN

> JavaScript has three different ways to say 'nothing'. Learn the critical differences between these absence values.

Subject: JavaScript | Lesson 8 of 63 | 5 min read
Interactive lesson: https://codewithanimation.in/javascript/null-undefined-nan
Previous: [Type Conversion (Explicit)](https://codewithanimation.in/javascript/type-conversion.md) | Next: [Conditionals: if, switch & ternary](https://codewithanimation.in/javascript/conditionals.md)

## The three absences

`undefined` means a variable has been declared but has not yet been assigned a value. It is JavaScript's default state of 'nothingness'.

`null` is an assignment value. It means you are intentionally stating that a variable holds 'nothing' or is 'empty'.

`NaN` stands for 'Not-a-Number'. It is the result of a mathematical operation that failed or an invalid type conversion (e.g. trying to divide a word by 2).

## Why have both null and undefined?

Having two empty values is one of JavaScript's most criticized design flaws, but it can be useful. `undefined` usually means the system or engine hasn't provided a value yet. `null` means the programmer explicitly set the value to be empty.

By keeping this convention, you can look at a variable and immediately know if it's empty because you made it empty (`null`), or empty because you forgot to initialize it (`undefined`).

## How to check for them

To check for `null` or `undefined`, always use strict equality (`=== null`). Do not use loose equality (`==`), because `null == undefined` evaluates to true!

Checking for `NaN` is uniquely difficult because `NaN === NaN` is actually false. `NaN` is the only value in JavaScript that is not equal to itself.

To reliably check if a value is `NaN`, use the built-in `Number.isNaN(value)` method.

## Common mistake: The typeof quirks

As discussed in previous lessons, `typeof null` returns `'object'` due to an ancient bug.

Ironically, `typeof NaN` returns `'number'`. Yes, JavaScript considers 'Not-a-Number' to be a numeric type. It simply represents an invalid number.

## Tip: Optional chaining fallback

When accessing deep object properties that might be `null` or `undefined`, use the nullish coalescing operator (`??`) to provide a fallback: `const name = user?.profile?.name ?? 'Anonymous';`.

## Try it: The Nothingness Matrix

Observe how these three empty values behave differently when compared, checked for type, or used in math.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/null-undefined-nan

## Check yourself

### Question 1

What is the result of `NaN === NaN`?

- A. false
- B. true
- C. undefined

Correct answer: A

`NaN` is never equal to `NaN`. You must use `Number.isNaN()` to check if a value is `NaN`.

- Why not B: Incorrect. NaN is the only value in JavaScript that is not equal to itself.
- Why not C: Incorrect. Strict equality always returns a boolean.

### Question 2

Which value indicates that a variable has been declared but not assigned?

- A. undefined
- B. null
- C. NaN

Correct answer: A

JavaScript engine defaults all uninitialized variables to `undefined`.

- Why not B: Incorrect. `null` must be explicitly assigned.
- Why not C: Incorrect. `NaN` is the result of failed math.

### Question 3

What does `typeof NaN` return?

- A. 'number'
- B. 'NaN'
- C. 'undefined'

Correct answer: A

Ironically, `typeof NaN` evaluates to `'number'`. It is technically a numeric data type, it just represents an invalid or unrepresentable number.

- Why not B: Incorrect. There is no 'NaN' type.
- Why not C: Incorrect. `NaN` is considered a numeric value.

## Remember this

- `undefined` means uninitialized. `null` means intentionally empty.
- `NaN === NaN` is always false. Use `Number.isNaN()` instead.
- `typeof null` is `'object'` and `typeof NaN` is `'number'`.
