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.
What is the difference between a variable that is: null, undefined or undeclared?
Quick Answer
Answer
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 |
JAVASCRIPT
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
JAVASCRIPT
const obj = {};
console.log(obj.age); // undefined (missing property)
function greet(name) {
console.log(name); // undefined when omitted
}
greet();
JAVASCRIPT
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.
Still so complicated?
undefined = empty box that exists. null = box intentionally marked "empty". undeclared = no box at all.
Summary
- undefined is a value.
- null is an intentional value choice.
- undeclared is usually a coding error.
- Use strict equality when comparing these states.
Similar questions
Guides
Preparing for interviews?
Use the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.