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.
Block Scope in Conditionals and Loops in JavaScript
Are let and const in an if block scoped to that block in JavaScript?
The answer
More FAQs in Block Scope in Conditionals and Loops in JavaScript
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.
No. try and catch are separate blocks. A let or const declared in the try block is not accessible in the catch block, and vice versa. The catch parameter (err) is scoped to the catch block only.
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.