This IIFE code is able to access count
let count = 0;
(function immediate() {
if (count === 0) {
let count = 1;
console.log(count);
}
console.log(count);
})();
But why is count undefined in this case?
var count = 0;
(function immediate() {
if (count === 0) {
var count = 1;
console.log(count);
}
console.log(count);
})();
If you use var
the variable declaration is hoisted, that mean that your code will be rewritted in the following way:
var count = 0;
(function immediate() {
var count; // decalration of variable hoisted on the function scope
// and the count variable now is undefined.
if (count === 0) {
count = 1;
console.log(count);
}
console.log(count); // just this is printed.
})();
As you can se, the variable inside the if block is now overriding the global count.
The fact that this behaviour is not so obvius is the reason why many suggest to avoid using var as we can use let and const instead, that have a block scope.
Update
Thanks to the comment of @t.niese I think is wort to mention another difference between var vs let and const.
Like var, let and const are hoisted, but with this differencese: