MediumHoisting💻 Output Question

Local var shadows outer — from start of function

💡

Hint

Local var declarations shadow the outer variable from the BEGINNING of the function — not just from where they appear.

What does this output?

var a = 1;
function test() {
  console.log(a);
  var a = 2;
  console.log(a);
}
test();
console.log(a);

Correct Output

undefined
2
1

Why this output?

Explanation: var a inside test() is hoisted — it shadows the global a from the start. First log: hoisted undefined. Then assigned 2. Global a stays 1.

Key Insight: Local var declarations shadow the outer variable from the BEGINNING of the function — not just from where they appear.

More Hoisting Output Questions

Easyvar declaration hoisted as undefinedEasyFunction declaration fully hoistedEasyFunction expression not hoistedMediumFunction declaration takes priority over var in hoisting

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz