undefined vs not defined in Javascript.
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