Working with a vanilla Javascript project, I stumbled upon a piece of code that declared multiple const inside a for...of loop. Trying to understand how it worked, I replicated it using an MDN example on the for...of docs:
const iterable = 'abc';
for (const value of iterable) {
const iterableAgain = value + " Again!";
console.log(iterableAgain);
console.log(value);
}
...the code below will show:
a Again!
a
b Again!
b
c Again!
c
This means that const is being re-declared each loop. How is this possible? I thought const and let couldn't be redeclared without throwing TypeError, besides some circumstances. Is this a case of variable shadowing? If so, does that mean that every iteration in a for...of consist of a different variable block?
Note: Stack Overflow threw me similar questions that have been asked, but to clarify, none have been made related plain Javascript, all of those have been asked related to Typescript.