Behaviour of const/variable created inside a loop

Hi someone, I need a little clarification here.
How does a variable declared within the block of a loop behave? Does it get created just once?

Shouldn’t i get an error on the next iteration that the variable has already been declared/cannot change constant assignment? Or is every loop iteration running in a different scope…

For instance,

//...
for (const arr of myArr) {// myArr contains objects
const product = new Product(arr) // shouldn't i get an error on the next iteration?
//...
}

I have searched the web, but i can’t find an answer. No one is even replying me here either!

Thanks in advance.

https://medium.com/@mautayro/es6-variable-declaration-for-loops-why-const-works-in-a-for-in-loop-but-not-in-a-normal-a200cc5467c2#:~:text=Const%20is%20special%2C%20because%20variables,t%20be%20reassigned%20or%20redeclared.&text=The%20our%20iterator%20i%20is,place%20to%20use%20let%20instead!

You basically already figured it out.

1 Like

Thanks for your response @RandellDawson