null
Introduction
null is a primitive value that usually means intentional absence: a value is expected, but it is deliberately empty. It is different from undefined, which usually means missing, uninitialised, or not provided.
The classic quirk is typeof null === 'object', but null itself is still a primitive value.
Why This Matters
APIs often use null to say no result, databases use it heavily, and JSON preserves it. Interviews use null to test equality rules, typeof quirks, and whether you can design clear missing-value semantics.
Theory
Intentional absence
Use null when your program deliberately sets a variable or property to no value. For example, a search result may be null when no user exists.
| Situation | Prefer | Reason |
|---|---|---|
| No selected item yet | null | Absence is intentional state. |
| Optional parameter omitted | undefined | JavaScript supplies it automatically. |
| Object property intentionally cleared | null | Communicates deliberate emptiness. |
| JSON API field with no value | null | JSON supports null, not undefined. |
typeof null quirk
typeof null returns 'object', but this is a historical bug. Do not use it as proof that null is an object. To check for object-like values, write value !== null && typeof value === 'object'.
Equality with undefined
null == undefined is true because loose equality treats them as the same kind of absence. null === undefined is false because they are different primitive values. In interviews and production code, prefer explicit checks unless you intentionally want to match both.
JSON behaviour
JSON.stringify keeps null object properties but omits properties whose value is undefined. In arrays, both undefined slots and null become null in JSON output.
Visual Diagrams
undefined -> not provided / not initialised / missing by default
null -> provided deliberately as empty
API response: { user: null } means the field exists and no user was foundUse `null` when absence is an intentional value in your domain model.
Code Examples
Checking for null explicitly
Strict equality makes the intent clear and avoids accidental matches with undefined.
JSON preserves null
null is part of JSON. undefined is not a JSON value for object properties.
Output Prediction
Predict the output #1
1console.log(typeof null);2console.log(null == undefined);3console.log(null === undefined);4console.log(JSON.stringify({ a: null, b: undefined }));5console.log(JSON.stringify([undefined, null]));Interview Questions
1When would you use `null` instead of `undefined`?
Use null when absence is intentional and meaningful in the domain: no selected item, no database row found, or a field deliberately cleared. undefined is better for values JavaScript naturally leaves missing: uninitialised variables, omitted arguments, missing properties, or functions without returns.
Follow-ups
- Why does `typeof null` return `object`?
- How does JSON treat `null` and `undefined` differently?
Quiz
1. Which statement is correct?
Summary
- `null` represents intentional absence and is a primitive value.
- `typeof null` returns `'object'` because of a historical bug.
- `null == undefined` is true, but `null === undefined` is false.
- JSON preserves `null` object properties but omits `undefined` object properties.
Cheat Sheet
Meaning: deliberate empty value / no object / no result.
Type quirk: typeof null === 'object'.
Equality: null == undefined true; null === undefined false.
JSON: null is preserved; object properties with undefined are omitted.
Guard: value === null for exactly null; value == null only if you intentionally mean null or undefined.