HardHoisting💻 Output Question

var escapes block, let does not

💡

Hint

var escapes blocks. let/const are block-scoped. typeof on a block-scoped variable outside its block returns "undefined".

What does this output?

function test() {
  if (true) {
    var x = 10;
    let y = 20;
  }
  console.log(x);
  console.log(typeof y);
}
test();

Correct Output

10
undefined

Why this output?

Explanation: var x escapes the if block (function-scoped). let y is block-scoped — inaccessible outside. typeof y returns "undefined" safely.

Key Insight: var escapes blocks. let/const are block-scoped. typeof on a block-scoped variable outside its block returns "undefined".

More Hoisting Output Questions

Easyvar declaration hoisted as undefinedEasyFunction declaration fully hoistedEasyFunction expression not hoistedMediumLocal var shadows outer — from start of function

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz