Undefined and Not defined in JS deep Dive

what is the difference between undefined and not defined both sounds same , well..

ยท

1 min read

Undefined and Not defined in JS deep Dive

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 :

output

why did this happen,๐Ÿง

This has to do with how javascript executes and manages programs.

Forget Will Smith GIF

  1. 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"

Cartoon Thumbs Up GIF by SpongeBob SquarePants

Thanks for reading it through, dont forgot to give a reaction ๐Ÿ˜

ย