undefined vs not defined in Javascript.

Rajesh Bhattarai
2 min readSep 27, 2021
Photo by Cintya Marisa on Unsplash

Now before I start If you don’t know what is hoisting then you won’t get much here. So I would recommend you to read my blog on hoisting first.

Considering you know hoisting. Let’s Begin

What is undefined in Javascript?

Undefined is a placeholder being assigned for the “var” variable in memory phase of an execution context. And during the code execution phase, the undefined is replaced by the actual value.

CODE:
console.log(x);
var x = 7;
OUTPUT:
undefined

What is not defined in Javascript?

When a variable is not available in the current context of execution, you get not defined. It simply means, if no variable is declared in your code and you try to access it you get not defined. It means there is no such variable in the program or the variable you mentioned is not defined in the program.

CODE:
console.log(x);
OUTPUT:
Reference Error: x is not defined

One more example…

CODE:
console.log(greet);
console.log(fruit);
var greet = "hello";
OUTPUT:
undefined // you get undefined for greet due to hoisting
Reference Error: fruit is not defined
// since we don't have fruit variable, hence not defined

Consider following me if you understood the concept. And if didn’t. It’s okay, we all need to go through the learning curve.

If you want more such content then do follow me on Twitter LinkedIn and Instagram.

--

--

Rajesh Bhattarai

I am a software developer who loves writing blogs about programming and web development.