Undefined and Not defined in JS deep Dive
what is the difference between undefined and not defined both sounds same , well..
Table of contents
undefined
= "Variable is there but no value is yet given to it"
not defined
\= " Variable is not declared"
Understanding part
console.log("a: ",a); // undefined
console.log("b: ",b); // not defined
var a = 20;
Output :
why did this happen,๐ง
This has to do with how javascript executes and manages programs.
Before the program is executed js allocates memory for all the functions and 'var' variables.
In the above image you can see 'a' is stored in global scope but its value is yet not defined when we are executing line '1'
after the line '4' is executed a now has a value of 20. So after line '4' value of 20
is given to a
.
and console.log("b: ",b)
gives us an error because js can not find a variable named 'b' in its global scope ( or in memory)
Therefore in conclusion
undefined
= "Variable is there but no value is yet given to it"not defined
\= " Variable is not declared"
Thanks for reading it through, dont forgot to give a reaction ๐