Given the following simplified code, why do I get a ReferenceError?
function foo(n){
for (let i = 0; i < n; i++) {
console.log(i);
let i = 10;
}
}
foo(4);
// OUTPUT: ReferenceError: i is not defined
let
variables should be hoisted as all other types of the declaration. Hence, in this code i
is declared twice. First, in the initialization close and second in the for loop itself.
Why does this output i is not defined
instead of Identifier 'i' has already been declared
.
How does JS read the code above?