Back to articles

JavaScript undefined vs. null vs. undeclared

12 min
Front-endJavaScript

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 value
  • null — 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:

JavaScript
let name;
console.log(name); // undefined

Function with no return value:

JavaScript
function greet() {
  console.log("Hello");
}

const result = greet();
console.log(result); // undefined

Accessing a property that doesn't exist:

JavaScript
const user = { name: "Charmy" };
console.log(user.age); // undefined

Function called without an expected argument:

JavaScript
function greet(name) {
  console.log(name); // undefined
}

greet();

typeof undefined

JavaScript
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.

JavaScript
let user = null; // no user yet, by choice

Common use cases:

JavaScript
// 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:

JavaScript
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:

JavaScript
console.log(x); // ReferenceError: x is not defined

This is completely different from undefined:

JavaScript
let x;
console.log(x); // undefined — declared, no value

console.log(y); // ReferenceError — never declared

The typeof Exception

typeof is special — it won't throw a ReferenceError on undeclared variables. It returns "undefined" instead:

JavaScript
typeof undeclaredVariable // "undefined" — no error

This makes typeof useful for safely checking whether a variable exists:

JavaScript
if (typeof someVariable !== "undefined") {
  // safe to use someVariable
}

How to Check for Each

Checking for undefined

JavaScript
let x;

x === undefined          // true
typeof x === "undefined" // true

Checking for null

JavaScript
let x = null;

x === null // true

Don't use typeof to check for null — you'll get "object", which is misleading:

JavaScript
typeof null === "object" // true — but this is a bug, don't rely on it

Checking for Both null and undefined

Use == (one of the rare valid uses):

JavaScript
let x = null;
let y;

x == null // true
y == null // true (undefined == null is true)

Or be explicit with ===:

JavaScript
if (value === null || value === undefined) {
  // ...
}

Checking for undeclared

Use typeof to avoid a ReferenceError:

JavaScript
if (typeof someVariable !== "undefined") {
  // someVariable exists and has a value
}

Quick Reference

undefinednullundeclared
MeaningDeclared, no value assignedExplicitly set to emptyNever declared
typeof"undefined""object""undefined"
Direct accessReturns undefinedReturns nullThrows ReferenceError
== nulltruetrueThrows ReferenceError
=== nullfalsetrueThrows ReferenceError

Conclusion

  • undefined — JavaScript's default empty value; the variable exists but hasn't been given a value yet
  • null — 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 null instead of leaving it undefined — the intent is clearer
  • When checking whether an unknown variable exists, use typeof instead of accessing it directly to avoid a ReferenceError