🟡 MediumClosures & Scope💻 Output Question

Block scope vs function scope

💡

Hint

var ignores block scope and shares with the function. let is block-scoped — the inner y is a completely separate variable.

What does this output?

function test() {
  var x = 1;
  let y = 2;

  if (true) {
    var x = 3;  // same var x — function scoped
    let y = 4;  // new y — block scoped
    console.log(x, y);
  }

  console.log(x, y);
}
test();

Correct Output

3 4
3 2

Why this output?

Explanation: Inside the block: var x=3 overwrites the outer x, let y=4 is new. After the block: x is still 3 (var leaked out), but y is 2 again (block let is gone).

Key Insight: var ignores block scope and shares with the function. let is block-scoped — the inner y is a completely separate variable.

More Closures & Scope Output Questions

🟢 EasyClassic var in loop closure→🟢 Easylet in loop closure (fix)→🟡 MediumClosure counter→🟡 MediumIIFE closure→

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz