JavaScript: `null` vs `undefined`
`null` represents an intentional absence of a value, while `undefined` commonly indicates that a value has not been assigned or is unavailable. Knowing their differences helps frontend engineers write clearer checks and avoid unexpected behavior.
`undefined` is a primitive value in JavaScript. A declared variable that has not received a value evaluates to `undefined`, and functions that do not explicitly return a value also return `undefined`.
`null` is also a primitive value, but it is typically used when code deliberately represents the absence of an object or other value. For example, setting `user = null` can explicitly indicate that there is currently no user object.
They behave differently with strict equality: `null === undefined` is `false`. Loose equality treats them as equal, so `null == undefined` is `true`. In most application code, strict equality is preferable because it makes the intended type distinction explicit.
A common frontend pattern is to use `value == null` when the requirement is specifically to detect either `null` or `undefined`. If the application needs to distinguish the two states, use `value === null` and `value === undefined` separately.
Key points
- `undefined` commonly means a value has not been assigned or is unavailable.
- `null` commonly represents intentional absence.
- `null === undefined` is `false`, while `null == undefined` is `true`.
- Use strict equality when you need to distinguish the two values.
- Use `value == null` deliberately when both `null` and `undefined` should count as missing.
Interview questions
What is the difference between `null` and `undefined` in JavaScript?
`undefined` commonly means that a value has not been assigned or is unavailable. `null` is an explicit value used to represent intentional absence. They are different values, so `null === undefined` is false. A developer should choose between them based on whether the empty state is implicit or deliberately represented.
What does `null == undefined` return, and what about strict equality?
`null == undefined` evaluates to `true` because JavaScript's loose-equality rules specifically pair these two values. `null === undefined` evaluates to `false` because strict equality does not perform that equivalence. In most code, strict equality is preferred when the exact value matters.
What happens when you access a missing object property?
Reading a property that does not exist normally produces `undefined`. For example, `user.name` evaluates to `undefined` when the `name` property is absent. This is different from a property that exists and has the value `null`. Code can therefore use strict checks when it needs to distinguish a missing property from an explicitly empty one.
Why can `typeof null` be surprising?
`typeof null` returns the string `"object"`, even though `null` is a primitive value. This is a long-standing behavior of JavaScript and should not be interpreted as evidence that `null` is an object. If you need to test specifically for null, use `value === null`.