undefined & not defined

Hello Everyone, in this blog we will see about undefined and not defined.

undefined

It's a JavaScript keyword that has a special meaning. Everything which gets space in memory it contains undefined until we assign value to that space.

1. console.log(x);
2. var x = 14;
3. console.log(x);

Output

undefined
14

Here we can see for line no 1, it printed undefined because we are trying to access that variable before initializing value in it.
And at line no 3, we are again trying to access variable x , this time it printed 14, because we are accessing it after putting some value in it.

So until putting some value in a variable it contains undefined, and after putting value in it undefined replaces with that value.


not defined

In JavaScript, It's one of the reference error that JavaScript throws when someone tries to access the variable which is not inside the memory.

1. var a = 10;
2. var b = 20;
3. 
4. console.log(a);
5. console.log(z);

Output

10
Uncaught ReferenceError : z is not defined.

Here we can see when we tried to access a at line no 4 it printed 10 in console, but at line no 5 we are trying to access z which is not inside the memory, so it threw an error z is not defined.