I've always been puzzled by this question: If block scopes are generated when a let
or const
identifier is enclosed within curly brackets, then how come the let
identifier in the initialization statement of a for
loop isn't accessible in the outer scope but is only available inside the curly brackets of the for
loop?
(function() {
for (let i = 0; i < 5; i++) {
console.log(i) // logs current value of i
}
console.log(i) // referenceError
})()