undefined means declared but currently no value, null means intentionally empty, and undeclared means the identifier does not exist in scope. Treat them differently in logic and debugging.
Use this JavaScript interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.
What is the difference between a variable that is: null, undefined or undeclared?Frontend interview answer
This JavaScript interview question tests whether you can explain null vs undefined vs undeclared in JavaScript, connect it to production trade-offs, and handle common follow-up questions.
- null vs undefined vs undeclared in JavaScript explanation without falling back to memorized docs wording
- Null and Undefined reasoning, edge cases, and production failure modes
- How you would answer the most likely JavaScript interview follow-up
Direct distinctionundefined: declared, no assigned value yet.null: explicitly set to no value.
undeclared: name does not exist in current scope.
State | Example | Read result |
|---|---|---|
undefined |
|
|
null |
|
|
undeclared |
| ReferenceError |
let a;
console.log(a); // undefined
let b = null;
console.log(b); // null
// console.log(c); // ReferenceError: c is not defined
Where undefined appears naturally
const obj = {};
console.log(obj.age); // undefined (missing property)
function greet(name) {
console.log(name); // undefined when omitted
}
greet();
console.log(undefined == null); // true
console.log(undefined === null); // false
console.log(typeof null); // 'object' (historical JS quirk)
Practical rule
Use null when you intentionally clear/reset a value. Treat undefined as "not provided yet". Treat undeclared as a bug (scope/typo/load-order).
Common pitfalls
- Using truthy checks and accidentally treating 0/false/'' as missing.
- Assuming
typeof nullshould be 'null'. - Catching undeclared errors instead of fixing declaration/scope.
undefined = empty box that exists. null = box intentionally marked "empty". undeclared = no box at all.
- undefined is a value.
- null is an intentional value choice.
- undeclared is usually a coding error.
- Use strict equality when comparing these states.
Use this as one explanation rep, then continue with the JavaScript interview questions cluster or a guided prep path.