Hoisting
Javascript literally hoists the variable declaration to the global scope.
console.log(a);
var a = 1;
// results in undefined
In practice, what happens is:
var a;
console.log(a);
a = 1;
- The variable declaration goes to the top of the scope;
- The variable attribution remains in place;
- In practice, the variable already exists, it just does not have a value yet;
If you instead declare your variables using let or const, you end
up witn an ReferenceError