Yes. The body of a while or do...while loop is a block. let and const inside it are block-scoped and not accessible outside the loop. Each iteration is a fresh block.
Block Scope in Conditionals and Loops in JavaScript
Do while loops create block scope for let and const in JavaScript?
The answer
More FAQs in Block Scope in Conditionals and Loops in JavaScript
Yes. let and const inside an if or else block are block-scoped. They are not accessible outside the block. Each branch of if...else is its own separate block scope.
Yes. Each iteration of for...of creates a fresh binding that does not change within the iteration, so const is fine. Use let only for classic for loops where the counter increments.
Because without braces, all cases share one block scope. Declaring let or const with the same name in two cases throws SyntaxError. Wrapping each case in braces gives it its own block scope.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.