This is one of the most underrated JavaScript concepts.
When I was learning JavaScript, I used to treat errors as noise.
Red text → fix somehow → move on.
But one error kept showing up again and again:

They sound very similar, but they are not.
Understanding this one distinction completely changed how I think about JavaScript execution and memory.
What undefined actually means
undefined is a special keyword in JavaScript.
When a variable is created in a JavaScript program, before the code execution a memory is allocated for that variable with undefined.
It literally means:
“JavaScript knows this variable exists, but no value has been assigned yet.”
Example :
Why?
Because during the creation phase of execution context, JavaScript does this:
- Sees var x
- Allocates memory for x
- Assigns a default value → undefined So when the code runs, x already exists in memory. That’s why undefined is logged in console.
undefined is not empty. It has its own memory space. It's a placeholder which is kept for the time being until the variable is assigned some other value.
Why not defined throws an error
Not defined is something which is not allocated in memory space.
For Example :

Here before the code execution, there is no memory allocated to y. So it throws an error as shown below :
![]()
This error doesn’t mean “value missing”. It means something much stronger:
“JavaScript has no idea what y is.”
During the creation phase, JavaScript is searching for declarations(var, functions) and allocates memory only for what it finds. Since y was never declared, JS never created space for it.
So when in code execution, JavaScript tries to access it → It throws error upfront.
The key difference
- undefined → memory exists, value doesn’t
- not defined → memory itself doesn’t exist
This is not a runtime guess. It’s decided before execution even starts.
How JavaScript decides this internally
Let’s slow this down. Consider this code:
In the creation phase, JavaScript stores the value of x as undefined ** in the memory. **y is ignored because it’s not declared anywhere.
In the execution phase, since x exists in memory so instead of throwing error it prints undefined. y does not exist in memory so JavaScript throws error.
Why JavaScript behaves this way
JavaScript is not a line-by-line interpreter.
It works in two phases:
- Memory allocation
- Execution
Errors like not defined are not about timing. They are about whether memory was ever allocated.
This exact concept becomes stricter with let and const. They do get memory, but JavaScript intentionally blocks access until initialization.
Warning
Never do this :

Why?
undefined is a placeholder which is kept inside the variable and it states that the variable is not assigned anything throughout the code. It meant for a specific purpose.
undefined and not defined are not small differences. They reveal how JavaScript thinks before it runs code.
Two similar words. Two completely different memory states.
That’s JavaScript.
If you liked this post, I’d love to hear your thoughts. Share your feedback in the comments and connect with me on LinkedIn.


Top comments (0)