Course contentsShow
← JavaScript
Lesson 32 of 2,7232. Values and TypesFree lesson

undefined: The Default Value

Understand when and why variables are undefined, and how undefined differs from undeclared.

undefined: The Default Value

You'll learn what undefined means in JavaScript and why variables start with this special value.

What is undefined?

In JavaScript, undefined is one of the seven primitive types. It's the automatic default value that JavaScript assigns to variables when you declare them but don't give them a value yet.

Think of it like an empty parking spot with a reserved sign: the spot exists and has a name, but no car is parked there yet. JavaScript knows about the variable, but it doesn't contain any actual data.

When Variables Are undefined

When you declare a variable without assigning it a value, JavaScript automatically sets it to undefined:

let myVariable;
console.log(myVariable); // undefined

The variable myVariable exists—JavaScript knows about it—but it hasn't been given a value, so it defaults to undefined.

undefined vs Undeclared

Here's a crucial difference:

  • undefined means the variable exists but has no value (yet)
  • Undeclared means the variable doesn't exist at all—you never created it
let emptyBox;
console.log(emptyBox); // undefined (box exists, nothing in it)

console.log(neverCreated); // Error! Variable doesn't exist

The first example shows a declared-but-empty variable. The second would cause an error because neverCreated was never declared with let, const, or var.

Why This Matters

JavaScript uses undefined to signal "this exists but has no value." It's different from intentionally setting something to empty—that's what null is for (a different primitive type).

Key Takeaway: undefined is JavaScript's way of saying "I know about this variable, but it doesn't have a value yet"—it's the default state for declared variables.