EasyClosures & Scope💻 Output Question

var leaks out of block, let does not

💡

Hint

var ignores block scope. typeof on a non-existent variable returns "undefined" instead of throwing.

What does this output?

{
  var a = 1;
  let b = 2;
}
console.log(a);
console.log(typeof b);

Correct Output

1
undefined

Why this output?

Explanation: var is function-scoped (or global here) — it escapes the block. let is block-scoped — b is inaccessible. typeof returns "undefined" safely without throwing.

Key Insight: var ignores block scope. typeof on a non-existent variable returns "undefined" instead of throwing.

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