What is the difference between null and undefined in JavaScript
· Category: JavaScript
Short answer
undefined means a variable has been declared but not assigned a value, while null is an intentional assignment representing no value.
Key differences
| Aspect | undefined | null |
|---|---|---|
| Type | 'undefined' |
'object' (legacy bug) |
| Meaning | Absence of initialization | Intentional absence of value |
| Default | Unassigned variables, missing properties | Must be assigned explicitly |
| Equality | undefined == null is true |
undefined === null is false |
When to use each
- Let the engine provide
undefinedfor uninitialized state. - Assign
nullto clear an object reference or indicate no result.
Example
let a;
console.log(a); // undefined
let b = null;
console.log(b); // null
console.log(a == b); // true
console.log(a === b); // false
Why it matters
Using === avoids confusion between the two. Prefer explicit null checks when you expect a developer-intended empty value.