null vs undefined in JavaScript — What's the Difference?
undefined means a variable was declared but never assigned; null is an explicit 'no value' assigned intentionally. Learn how they compare and when each appears.
Published September 16, 2026
undefined means a variable exists but has no value yet; null is a value you assign on purpose to say 'nothing here'. Both are falsy, but they are not strictly equal.
The concept
JavaScript itself produces undefined: uninitialised variables, missing object properties, functions with no return and missing arguments all give undefined.
null is never produced by the language for you; a developer assigns it to represent an intentional empty value, such as 'no user selected'.
Where each one comes from
Practical examples
Check for either with ??
const name = user.name ?? 'Guest' // used when null OR undefinedOptional chaining
const city = user?.address?.city // undefined if anything is missingtypeof quirk
typeof undefined // 'undefined'
typeof null // 'object' (historic bug)Common mistakes
- Using || for defaults, which also replaces 0 and '' — use ?? instead.
- Relying on typeof null === 'object' to detect objects.
- Mixing == and === without knowing null == undefined is true.
Key takeaways
- undefined = not set; null = deliberately empty.
- ?? and ?. treat both the same way.
- Use === unless you intend the loose null check (x == null).
FAQ
Should I assign undefined to a variable?
Avoid it; use null to mean empty on purpose so intent is clear.
How do I check for both quickly?
value == null is true only for null and undefined, or use value ?? fallback.