These two sound alike but mean completely different things. Confusing them is one of the most common beginner mistakes.
You can read undefined vs not defined and try the first 1 exercise on the free plan. Upgrade to unlock the remaining 2 exercises and continue the full lesson flow.
Pro: every unit of every course · unlimited hearts · no weekly limit on assessment attempts
Complete lesson & earn 250 PX
EXERCISE
1When JavaScript says a variable is undefined, it does not mean the variable does not exist. It means memory was reserved, but nobody filled it with a value yet.
undefined is a value. That sentence does more work than it looks like it does, because most of the confusion around it comes from people treating it as an error or as nothing at all. It is neither. It is a real value that JavaScript puts in a slot to mean this name exists and nothing has been assigned to it yet.
You can check its type, store it, pass it to a function and compare against it, exactly like any other value:
var pending;
console.log(pending); // undefined
console.log(typeof pending); // "undefined"
console.log(pending === undefined); // true
There are three separate ways a value ends up as undefined, and knowing which one you are looking at tells you where the bug is.
A declared variable before its assignment, which is the case above, and also every var read before its line runs.
A property that does not exist on an object:
var user = { name: "Ada" };
console.log(user.name); // "Ada"
console.log(user.email); // undefined
Asking for a missing property is not an error. The object simply does not have it, and JavaScript answers with undefined rather than throwing. That is convenient right up until a typo in a property name gives you undefined instead of telling you the name was wrong.
A function that returns nothing:
function logIt(value) {
console.log(value);
}
var result = logIt("hi");
console.log(result); // undefined
logIt has no return, so calling it produces undefined. The function did its job; there was simply no value to hand back.
All three are ordinary behaviour, not failure. What makes undefined worth this much is the contrast coming in the next exercise: there is a second state that looks similar in a console and behaves completely differently, and mixing them up sends you looking in the wrong place.
One more source worth knowing, because it looks like a bug the first time you meet it:
function greet(name, greeting) {
console.log(greeting + ", " + name);
}
greet("Ada"); // "undefined, Ada"
A parameter you do not pass is not an error. It is declared by the function signature, so it has a slot, and the slot is empty: undefined again. will happily concatenate that into a string and show it to someone. Default parameter values exist precisely to close this gap:
function greet(name, greeting = "Hello") {
console.log(greeting + ", " + name);
}
greet("Ada"); // "Hello, Ada"
undefined means memory was allocated but no value assigned yet. not defined means the variable was never declared anywhere. typeof is the safe way to check.