Facebook Pixel

Block Scope in Conditionals and Loops in JavaScript

if, for, while, and switch all create blocks. Here is how let and const behave in each.

Block Scope in Conditionals and Loops in JavaScript

if, for, while, and switch all involve blocks. Here is how let and const behave in each.

if...else

if (condition) { const result = computeA(); let temp = result * 2; } else { const result = computeB(); // OK, separate block } console.log(result); // ReferenceError (block-scoped) `` Each branch of `if...else` is its own block. `result` in the `if` and `result` in the `else` are separate variables. ### `for` Loops ```js for (let i = 0; i < 3; i++) { const item = items[i]; // block-scoped to this iteration console.log(item); } console.log(i); // ReferenceError console.log(item); // ReferenceError `` `i` and `item` are both block-scoped. `let i` creates a fresh binding per iteration. ### `for...of` and `for...in` ```js for (const item of [1, 2, 3]) { console.log(item); // 1, 2, 3 } for (const key in { a: 1, b: 2 }) { console.log(key); // "a", "b" } `` `const` works here because each iteration is a fresh binding that does not change within the iteration. ### `while` and `do...while` ```js let count = 0; while (count < 3) { const item = `item ${count}`; // block-scoped to this iteration count++; } console.log(item); // ReferenceError `` ### `switch` ```js switch (x) { case 1: { const y = 1; // block-scoped to this case break; } case 2: { const y = 2; // OK, separate block break; } } `` Without braces, all cases share one block scope, and re-declaring `let` or `const` throws. Wrap each case in braces to give it its own scope. ### `try...catch` ```js try { const data = JSON.parse(json); // block-scoped to try } catch (err) { // err is block-scoped to catch console.log(err.message); } console.log(data); // ReferenceError console.log(err); // ReferenceError `` ### The Takeaway `if`, `for`, `while`, `switch`, and `try...catch` all create blocks. `let` and `const` inside them are block-scoped. Each `if`/`else` branch and each `switch` case (with braces) is a separate scope. `const` works in `for...of`/`for...in` (fresh binding per iteration). Wrap `switch` cases in braces to avoid re-declaration errors.

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.

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.

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.

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.