null vs undefined in JavaScript
JavaScript has two 'empty' values, and interviewers love to check you know the difference precisely.
What each means
undefined is what the engine gives you: uninitialised variables, missing parameters, and absent object properties. null is an intentional 'no value' you assign yourself to signal emptiness.
1let x; // undefined
2const y = null; // intentional empty
3null == undefined; // true
4null === undefined; // false
5typeof null; // "object" (historic bug)
6typeof undefined; // "undefined"Working with both safely
Use `x == null` to catch both at once, and nullish coalescing `x ?? fallback` to default only when a value is null or undefined (unlike ||, which also triggers on 0 and '').
Nail the JavaScript fundamentals
Types, coercion, equality and the gotchas — 80+ questions with clear answers in the JavaScript Interview Kit.
⚡ Get the JavaScript Interview Kit → ₹299Frequently asked questions
- Why is typeof null 'object'?
- It's a historic bug in JavaScript kept for backward compatibility. The only reliable null check is x === null.
- Should I return null or undefined?
- Prefer null when you intentionally mean 'no value'. Let undefined represent 'not set'. Be consistent within a codebase.
