🔴 HardClosures & Scope💻 Output Question

Closure variable lookup

💡

Hint

Closures capture the variable from their specific lexical scope, not outer scopes with the same name.

What does this output?

let x = 'global';

function outer() {
  let x = 'outer';
  function inner() {
    console.log(x);
  }
  return inner;
}

const fn = outer();
x = 'changed';
fn();

Correct Output

outer

Why this output?

Explanation: inner closes over the x in outer's scope ('outer'), not the global x. Even though global x changed to 'changed', inner's closure references outer's x which never changed.

Key Insight: Closures capture the variable from their specific lexical scope, not outer scopes with the same name.

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