Back to JavaScript Notes
Topic #354

undefined

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 undefined when no return value is provided.
  • It shows why reading a missing object property gives undefined instead of throwing an error.
  • It prevents bugs caused by treating undefined as if it were null or false.
  • It supports safer checks using x === undefined or typeof 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
  1. Declare the variable with let x; or var x;.
  2. Read the variable with console.log(x);.
  3. Check the value with x === undefined or typeof 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 undefined with null. Use null when you intentionally mean "no value"; undefined usually means "not set yet" or "missing".
  • Using x == undefined. This also matches null. Use x === undefined when you need an exact check.
  • Assuming every undefined comes from an unassigned variable. It can also come from a missing property or a function with no return value.
  • Trying to use undefined as 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.

ValueMeaningUse when
undefinedJavaScript has not assigned a value yet.Variables, missing properties, or functions with no return value.
nullThe 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.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

undefined – FAQs

Quick answers about learning undefined in JavaScript.

This free note from Coding Now Tech Institute explains undefined in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including undefined, is 100% free with no signup required.
With focused practice, most students grasp undefined in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now