Any code wrapped in a pair of braces {}. Blocks appear in if statements, for and while loops, try...catch, and as standalone blocks. let and const declared inside any of these are block-scoped.
What Is Block Scope in JavaScript?
What counts as a block in JavaScript?
The answer
More FAQs in What Is Block Scope in JavaScript?
Variables declared with let and const inside a pair of braces are only accessible inside those braces. var is not block-scoped; it is function-scoped and leaks out of blocks.
Yes. var is function-scoped, not block-scoped. A var inside an if or a standalone block is hoisted to the enclosing function or global scope. let and const do not leak.
Yes. The err in catch (err) is block-scoped to the catch block. It is not accessible outside the catch, and it does not leak to the outer 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.