By the end of this lesson, you will be able to recognize undefined in JavaScript as the default value of a declared variable that has not been assigned, and distinguish it from null and undeclared variables.
What it is
undefined is a primitive value in JavaScript. The mental model is a labeled box that has been created but not filled. When you declare a variable with let or var and do not assign a value, JavaScript initializes it with undefined. An undeclared variable is different: reading it throws a ReferenceError, although typeof undeclared returns "undefined". Related terms include uninitialized variable, missing object property, function with no return statement, and the typeof operator.
Why it matters
- It helps you spot variables that were declared but never set.
- It explains why a function can return
undefinedwhen no return value is provided. - It shows why reading a missing object property gives
undefinedinstead of throwing an error. - It prevents bugs caused by treating
undefinedas if it werenullorfalse. - It supports safer checks using
x === undefinedortypeof x === "undefined".
Syntax or steps
The smallest useful pattern is to declare a variable without assigning it, then read it.
let x;
console.log(x); // undefined
- Declare the variable with
let x;orvar x;. - Read the variable with
console.log(x);. - Check the value with
x === undefinedortypeof x === "undefined".
Example
let username;
console.log(username); // undefined
function greet(name) {
if (name) {
return "Hello, " + name;
}
}
console.log(greet()); // undefined
const user = { id: 1 };
console.log(user.email); // undefined
console.log(typeof username); // "undefined"
Part-by-part explanation
username is declared but not assigned, so its value is undefined. The function greet has no return statement when name is missing, so calling greet() produces undefined. The object user has no email property, so user.email is undefined. Finally, typeof username returns the string "undefined", which is a safe way to test the value's type.
Common mistakes
- Confusing
undefinedwithnull. Usenullwhen you intentionally mean "no value";undefinedusually means "not set yet" or "missing". - Using
x == undefined. This also matchesnull. Usex === undefinedwhen you need an exact check. - Assuming every
undefinedcomes from an unassigned variable. It can also come from a missing property or a function with no return value. - Trying to use
undefinedas a normal variable name. It is a global value, not a safe identifier for your own data.
When to use it
Compare undefined with null when deciding what a missing value means.
| Value | Meaning | Use when |
|---|---|---|
undefined | JavaScript has not assigned a value yet. | Variables, missing properties, or functions with no return value. |
null | The programmer intentionally set the value to "nothing". | Empty results, cleared references, or explicit absence. |
Use undefined when you are observing JavaScript's default behavior. Use null when you want to communicate a deliberate empty value.
Practice
Guided exercise: declare let score;, log it, then assign score = 10; and log it again. Expected output: undefined then 10.
Challenge: write a function called findUser that returns undefined when no user is found. Hint: do not add a return statement in that branch, or explicitly return undefined.
Quick check
Question: What is the value of let x; before any assignment?
Answer: undefined.
Summary
undefined is JavaScript's default value for a declared but unassigned variable. It also appears for missing properties and functions with no return value. Recognizing it helps you separate accidental missing data from intentional empty values such as null.