Redeclare let and const inside a for loop

Viewed 44

We know that let and const are not hoisted.

So code like this throws an error

let x = 33;
let x = 99;

SyntaxError: Identifier 'x' has already been declared

Now since let variables are limited to the scope of a block statement, something like the following is assumed to generate the same error as the code above!

for (let i = 0; i < 10;) { // i here declared in this for loop block
    let i = 11; // i here is in the same for loop block
    console.log(i);
    break;
}

But surprisingly does not!

output

11

Why let i here is treated like it's declared in a different block? or it's like a totally different variable?

0 Answers
Related