All 28 lessons ▾

Inside JavaScript · Lesson 6 of 28

undefined vs not defined

Updated 11 Nov 2026 · By Akshay, software engineer, India · 8 min read

Undefined, and not defined. They sound almost the same. But they're completely different, and mixing them up confuses a lot of people. So let's settle it, once and for all.

The video for this lesson is coming soon.

A new lesson goes up every day at 11 am. Subscribe on YouTube so you don't miss it.

Got a doubt? Ask in the comments under the video.Subscribe

In one minute
  • undefined: memory reserved, no value yet
  • not defined: never declared, a ReferenceError
  • undefined is a real placeholder value, not empty
  • JavaScript is loosely typed: any value, any time
  • Never assign undefined yourself; use null for empty

Part 1Two words, often confused

So we've already seen undefined a few times in this course. Every var gets it in phase one. But today, we go deep. So what is undefined, really? And how is it different from not defined? Let's see it with real code first, line by line.

marks.js
1console.log(marks);2var marks = 95;3console.log(marks);4 5console.log(grade);
Output
undefined
95
ReferenceError: grade is not defined

So look at this program. In phase one, marks gets a reserved spot in memory, with the placeholder in it. But grade? There's no var grade anywhere, so it gets nothing. Now phase two. Line one logs marks. It's in memory, but it has no real value yet, so we see undefined. Line two, marks becomes ninety five. Line three logs it again, and now it prints ninety five. Finally, line five logs grade. JavaScript looks in memory, and grade isn't there at all. So we get an error. A reference error. grade is not defined. You see the difference?

undefined
a reserved seat, still empty
not defined
no seat at all
the name card is there
ReferenceError
REMEMBER THIS
undefined:memory
reserved,novalueyet.
notdefined:notin
memoryatall.
not defined is an error, undefined is a
value

So remember this. Undefined means JavaScript has reserved memory for the variable, but no value has been assigned yet. Not defined means the variable was never declared, so there's nothing in memory at all. Now look at the difference in what happens. Undefined is just a value that gets printed. Not defined is an error that stops your program. Two very different things.

Now remember our wedding hall? Undefined is a seat with a name card on it. The seat is reserved, the card is there, but the guest hasn't arrived yet. And not defined is when the guest isn't even on the guest list. There's no seat, and no card. So when you go looking for them, the hall manager says, sorry, no such guest. That's your reference error. So a reserved empty seat, versus no seat at all.

Part 2undefined is not empty

Now, here's something many people get wrong. They think undefined means empty, or nothing. But it doesn't. So let's see why.

placeholder.js
1var a;2console.log(a);3console.log(typeof a);4console.log(a === undefined);
Output
undefined
undefined
true

So look at this. We declare a, but never give it a value. In phase one, a gets undefined, and that undefined actually sits there in memory, taking up space. Now line two logs a, and it prints undefined. And line three asks for its type. The type of undefined is also called undefined. And line four compares a with undefined itself, using triple equals. And it says true! So undefined is a real, special value in JavaScript. Most other languages don't even have it. A placeholder that says, this variable exists, but nothing has been put in it yet. It's a placeholder. Remember this.

sources.js
1function greet(name) {2  console.log(name);3}4greet();5 6var user = { city: "Goa" };7console.log(user.age);8 9function noReturn() {}10console.log(noReturn());

Now, where does undefined show up in real code? Mostly in three places, and you'll meet all three very soon. One. You call a function, but forget to pass an argument. Here, greet is called with nothing, so inside greet, name is undefined. Two. You read a property an object doesn't have. user has a city, but no age, so user.age gives undefined. And three. A function that doesn't return anything. Call noReturn, and what comes back is undefined. So whenever you see undefined in your console, ask yourself these three questions. Did I pass everything? Does this property exist? Did my function return something? nine times out of ten, the answer is right there.

BONUS
undefined:notassigned
yet.null:empty,on
purpose.
JavaScript sets undefined, you set null

And now a small bonus, because interviewers love this one too. So what's the difference between undefined and null? Undefined is what JavaScript gives a variable that hasn't been assigned yet. Null is something you set yourself, on purpose, to say this is empty. So JavaScript sets undefined, and you set null. Easy to remember. JavaScript's placeholder, versus your own empty value. Two different jobs.

Part 3JavaScript is loosely typed

Now, while we're talking about values, let me show you one more special thing about JavaScript. And it explains why undefined can sit in any variable.

loose.js
1var item = 5;2item = "five";3item = true;4 5console.log(item);
Output
true

So now watch the memory. Phase one, item gets undefined. Line one, item becomes five, a number. Line two, item becomes the word five, a string. And line three, item becomes true, a boolean. Number, string, boolean, all in the same variable! And JavaScript has no complaint at all. Line five prints true, the last value.

REMEMBER THIS
JavaScriptislooselytyped:avariable
isn'ttiedtoonetype.
also called weakly typed

So that's because JavaScript is a loosely typed language. Some people say weakly typed. It means a variable is not tied to one data type. Now in languages like Java or C plus plus, if you say a variable is a number, it stays a number forever. But in JavaScript, a variable is just a name, and it can hold any type of value, at any time. And weakly typed doesn't mean weak! The engine does a lot of work behind the scenes to juggle all these types for you. So that's flexible and powerful, but it also means you have to be careful.

Part 4Never set undefined yourself

Now, here's one rule I really want you to follow in your own code.

rule.js
1var user = undefined;   // don't do this2 3var cart = null;       // empty, on purpose4 5if (score === undefined) {6  // score has no value yet7}

So look at the first line. Somebody sets user equals undefined, by hand. So please don't do this. Undefined has a special job. It tells you that a variable hasn't been given a value yet. Now if you start putting undefined in variables yourself, you lose that signal. Later, when you're debugging, you can't tell whether a value was never set, or whether someone set it to undefined on purpose. So if you want to say empty, use null, like the cart here. And checking for undefined is totally fine, like the if on line five. Just never assign it yourself. Let JavaScript do that job.

REMEMBER THIS
Neverassignundefined
yourself.Usenullfor
empty.
let undefined keep its meaning

So remember this rule. Never assign undefined to a variable yourself. Let it happen on its own. And when you mean empty, on purpose, use null. your future self, debugging at midnight, will thank you.

safe-check.js
1console.log(typeof planet);2 3if (typeof planet === "undefined") {4  console.log("no planet yet");5}
Output
undefined
no planet yet

And now here's a pro tip. What if you want to check whether something exists, without crashing? planet is not declared anywhere. If you log planet directly, you'd get a reference error. But typeof is special. Line one asks for the type of planet, and instead of crashing, it gives back undefined. So line three can safely check it, line four runs, and it prints no planet yet. So typeof is the safe way to test a name that may not exist. Nice trick.

INTERVIEW ANSWER
undefined:declared,novalueyet.not
defined:neverdeclared.
the first is a value, the second a ReferenceError

Part 5Quick quiz and recap

So quick quiz first, then the recap. So pause the video if you want to think.

Quick quiz
quiz.js
1console.log(city);2var city = "Jaipur";3 4console.log(pin);

So what does line one print?

Show the answer

City is a var, so in phase one it got the placeholder. Line one prints undefined. And line four? There's no pin anywhere in the program. It's not in memory at all. So that's a reference error, pin is not defined. If you got both right, you've completely understood today's episode. Well done, seriously!

Your turn

Have you ever assigned undefined by hand?

Answer in the comments under the video.

Your progress stays on this device.
A new lesson every day at 11 am

The video on YouTube, and the lesson as a swipe-through on Instagram.