JavaScript undefined vs. null vs. undeclared
undefined, null, and undeclared are three concepts that look similar but mean very different things.
undefined— the variable exists, but hasn't been assigned a valuenull— the variable exists, and has been explicitly set to "empty"- undeclared — the variable doesn't exist at all
undefined
undefined is a primitive value in JavaScript. It means a variable has been declared, but no value has been assigned to it yet.
When You Get undefined
Variable declared but not assigned:
let name;
console.log(name); // undefinedFunction with no return value:
function greet() {
console.log("Hello");
}
const result = greet();
console.log(result); // undefinedAccessing a property that doesn't exist:
const user = { name: "Charmy" };
console.log(user.age); // undefinedFunction called without an expected argument:
function greet(name) {
console.log(name); // undefined
}
greet();typeof undefined
typeof undefined // "undefined"null
null is also a primitive value, but it means something different from undefined. It represents an intentional absence of value — someone explicitly set it to empty.
let user = null; // no user yet, by choiceCommon use cases:
// initializing a variable that will be set later
let selectedItem = null;
// API response indicating no data
const response = { data: null };
// function returning null when nothing is found
function findUser(id) {
const user = db.find(id);
return user || null;
}typeof null
null has a well-known historical quirk:
typeof null // "object"This is a bug from JavaScript's early days that's been kept for backward compatibility. null is not an object — typeof null === "object" is simply wrong, and shouldn't be relied on.
undeclared
Undeclared isn't a value — it's a state. It means the variable was never declared at all.
Accessing an undeclared variable throws a ReferenceError:
console.log(x); // ReferenceError: x is not definedThis is completely different from undefined:
let x;
console.log(x); // undefined — declared, no value
console.log(y); // ReferenceError — never declaredThe typeof Exception
typeof is special — it won't throw a ReferenceError on undeclared variables. It returns "undefined" instead:
typeof undeclaredVariable // "undefined" — no errorThis makes typeof useful for safely checking whether a variable exists:
if (typeof someVariable !== "undefined") {
// safe to use someVariable
}How to Check for Each
Checking for undefined
let x;
x === undefined // true
typeof x === "undefined" // trueChecking for null
let x = null;
x === null // trueDon't use typeof to check for null — you'll get "object", which is misleading:
typeof null === "object" // true — but this is a bug, don't rely on itChecking for Both null and undefined
Use == (one of the rare valid uses):
let x = null;
let y;
x == null // true
y == null // true (undefined == null is true)Or be explicit with ===:
if (value === null || value === undefined) {
// ...
}Checking for undeclared
Use typeof to avoid a ReferenceError:
if (typeof someVariable !== "undefined") {
// someVariable exists and has a value
}Quick Reference
| undefined | null | undeclared | |
|---|---|---|---|
| Meaning | Declared, no value assigned | Explicitly set to empty | Never declared |
| typeof | "undefined" | "object" | "undefined" |
| Direct access | Returns undefined | Returns null | Throws ReferenceError |
== null | true | true | Throws ReferenceError |
=== null | false | true | Throws ReferenceError |
Conclusion
undefined— JavaScript's default empty value; the variable exists but hasn't been given a value yetnull— an intentional empty value set by the developer; means "this is deliberately empty"- undeclared — the variable doesn't exist; accessing it throws a
ReferenceError
A couple of practical tips:
- When initializing a variable you'll fill in later, use
nullinstead of leaving itundefined— the intent is clearer - When checking whether an unknown variable exists, use
typeofinstead of accessing it directly to avoid aReferenceError