undefined vs not defined in JavaScript

undefined vs not defined in JavaScript

undefined !== not defined

Let us understand the difference between undefined and not defined
Let us start
Look at the code snippet below

let a = 1;
console.log(a) //1

Here, We have just assigned 1 to variable a. And if we console.log , we get output as 1. Nothing magical!

output:

console (2).png

Now, let us see another example We declared variable a and tried accessing it.

let a;
console.log(a) //undefined

Let us first see how our output looks like!

undefined (2).png

Strange! Now, Let us first understand what is "undefined".
undefined is a place holder used in JavaScript, but how is it used?
Everything in JavaScript is done in Global Execution Context, before even executing a single of code lot happens in the back ground. Every variable is assigned with undefined ,before it is actually assigned it's value.

let a;
console.log(a); //undefined
a= 10;
console.log(a); //10

when we are trying to access a ,undefined is printed it simply suggests we have a variable called a in our program ,but it is not assigned any value.

output :

Screenshot (6).png If the variable is not assigned any value then ,it will remain as undefined forever.
Now what is not defined then,
Let us look at another example

let a;
console.log(i);

We declared a ,and trying to access i .
what will be the output? try giving a guess!
And here it is,

output :

Screenshot (8).png

In contrast to undefined , not defined is shown when we are trying to access variable i which doesn't even exist in our program.
YAY! You got it now.
You might have a question like Why should we know all these ,but helps when you are debugging. You will be able to easily solve your error/bug if you know what it is actually.
HOPE THIS HELPS!
HAPPY LEARNING!
Do comment your views below.