JavaScript Basic
What are the differences between null, undefined, and undeclared?
Differences Between null, undefined, and undeclared
undefined
A variable that has been declared but not yet assigned a value:
let a;
console.log(a); // undefined
function foo(x) { return x; }
foo(); // undefined (no argument passed)
null
An intentionally assigned empty value meaning "no object" or "no value":
let user = null; // explicitly means no user currently
console.log(typeof null); // 'object' (historical bug in JS)
undeclared
A variable that has never been declared — accessing it throws a ReferenceError:
console.log(b); // ReferenceError: b is not defined
Use typeof to safely check for undeclared variables:
typeof b; // 'undefined' (no error thrown)
Comparison Table
| Feature | null |
undefined |
undeclared |
|---|---|---|---|
| Type | object |
undefined |
ReferenceError |
| Meaning | Intentionally empty | Declared but not assigned | Never declared |
| How it occurs | Manually assigned | Default value | — |
| Equality | null == undefined → true |
null === undefined → false |
— |
✦ AI Mock Interview
Type your answer and get instant AI feedback
Sign in to use AI scoring
