🟑 MediumClosures & ScopeπŸ’» Output Question

Block scope with let

πŸ’‘

Hint

typeof on undeclared variables returns "undefined" (doesn't throw). let is block-scoped, var is not.

What does this output?

{
  let a = 1;
  var b = 2;
}

console.log(typeof a);
console.log(typeof b);

Correct Output

undefined
number

Why this output?

Explanation: let is block-scoped β€” a is not accessible outside the block. typeof a returns "undefined" without throwing. var leaks out of the block.

Key Insight: typeof on undeclared variables returns "undefined" (doesn't throw). let is block-scoped, var is not.

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