MediumClosures & Scope💻 Output Question

var inside function shadows outer

💡

Hint

A local var declaration shadows the outer variable from the start of the function — not from where it appears.

What does this output?

var x = 'global';
function test() {
  console.log(x);
  var x = 'local';
  console.log(x);
}
test();

Correct Output

undefined
local

Why this output?

Explanation: var x inside test() is hoisted to the top of the function. First log sees the hoisted-but-uninitialized x (undefined), not the outer global.

Key Insight: A local var declaration shadows the outer variable from the start of the function — not from where it appears.

More Closures & Scope Output Questions

EasyClassic var in loop with setTimeoutEasylet in loop — each iteration gets own bindingMediumIIFE captures loop variableMediumClosure mutation persists across calls

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz