Facebook Pixel

What Is Block Scope in JavaScript?

A block is a pair of braces. let and const are scoped to it. Here is how block scope works and why it matters.

What Is Block Scope in JavaScript?

A block is any code wrapped in a pair of braces {}. Block scope means variables declared with let and const inside a block are only accessible inside that block.

What a Block Is

{ const x = 1; let y = 2; var z = 3; } `` The `{}` here is a block. It is not a function. Blocks appear in `if`, `for`, `while`, `try...catch`, and standalone. ### `let` and `const` Are Block-Scoped ```js { const x = 1; let y = 2; } console.log(x); // ReferenceError console.log(y); // ReferenceError `` `let` and `const` are confined to their block. ### `var` Is Not Block-Scoped ```js { var z = 3; } console.log(z); // 3 (leaked out) `` `var` is function-scoped. It ignores block boundaries (except function bodies). This is the key difference. ### Blocks in `if` Statements ```js if (true) { const message = "yes"; let count = 1; } console.log(message); // ReferenceError console.log(count); // ReferenceError `` ### Blocks in Loops ```js for (let i = 0; i < 3; i++) { const item = `item ${i}`; } console.log(i); // ReferenceError console.log(item); // ReferenceError `` ### Blocks in `try...catch` ```js try { const data = JSON.parse(json); } catch (err) { // err is block-scoped to the catch console.log(err.message); } console.log(data); // ReferenceError console.log(err); // ReferenceError `` ### Why Block Scope Matters - **Encapsulation**: keep variables confined to where they are used. - **Avoid collisions**: same name in different blocks does not conflict. - **Memory**: the engine can free block-scoped variables when the block ends. - **Safer loops**: `let` in a loop creates a fresh binding per iteration (fixes closure bugs). ### The Takeaway Block scope means `let` and `const` are confined to their `{}`. `var` ignores blocks and leaks out. Blocks appear in `if`, `for`, `while`, `try...catch`, and standalone. Block scope enables encapsulation, avoids collisions, and fixes loop closure bugs.

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.

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.

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.

It provides encapsulation (keep variables confined), avoids naming collisions, lets the engine free memory when the block ends, and fixes the classic var loop closure bug by giving each iteration a fresh binding.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.