Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 77 ยท JavaScript Interview Questions

JavaScript: Scope, Hoisting and Closures

Answer JavaScript interview questions on var, let and const, the temporal dead zone, hoisting, closures in loops and the module pattern with runnable code.

Scope and closures are the second most common JavaScript topic in interviews. They appear as concept questions ("What is hoisting?"), as predict-the-output puzzles (the loop with setTimeout), and as design questions ("How would you create private state?"). All of them reduce to one idea: where a variable lives and who can still reach it.

For the full tutorials see Scope and Hoisting and Closures. This lesson is about giving short, correct answers.

var, let and const

Three ways to declare

var Function scope. Hoisted and set to undefined.
let Block scope. Reassignable.
const Block scope. Cannot be rebound.

Default to const, use let when it must change, avoid var.

1 / 2

Q: What is the difference between var, let and const? var is function-scoped and its declaration is hoisted with the value undefined. let and const are block-scoped and sit in the temporal dead zone until their declaration runs. const forbids reassigning the binding, but the value it refers to can still be mutated.

JS Function scope versus block scope

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Hoisting and the temporal dead zone

Q: What is hoisting? Before running a scope, the engine registers its declarations. So var variables exist from the start of the function (value undefined), and function declarations exist with their full body. let, const and class are also registered, but you cannot touch them before their declaration line: that interval is the temporal dead zone (TDZ), and access throws a ReferenceError.

It is not accurate to say code "moves to the top". Nothing moves; the engine just knows the names in advance.

JS Hoisting in practice

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Follow-up: "Is a function expression hoisted?" Only its variable name (as undefined for var, or in the TDZ for let/const), not the function value.

Pitfall: typeof does not protect you inside the TDZ: typeof late before let late still throws.

Closures

Q: What is a closure? A function bundled with the variables of the scope where it was created. The function keeps access to those variables even after the outer function returns. The closure holds the variable itself, not a copy of its value.

JS Counters with private state

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Common uses to mention: private state, function factories, memoization, partial application, callbacks and event handlers that need context.

The loop question

The classic puzzle: what does this print?

JavaScript
for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0);
}

All callbacks run after the loop finishes, and they all share the one function-scoped i, which is now 3. So it prints 3 three times. With let, each iteration gets its own binding, so the same code prints 0, 1, 2.

JS Closures in loops: three fixes

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Pitfall: closures keep their variables alive. A long-lived closure that references a large array prevents it from being garbage collected, which is a common cause of memory growth in browsers. Release references you no longer need.

The module pattern

Before ES modules, the module pattern used an immediately invoked function to create private scope and return a public interface. The same technique is still a good answer to "How do you create private data without classes?"

JS The module pattern

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Mention modern alternatives: ES modules (module-level variables are private unless exported) and class private fields (#items), covered in JavaScript: this, Prototypes and Classes.

Scope chain and shadowing

When a variable is read, the engine looks in the current scope, then the enclosing scope, and so on to the global scope. This is the scope chain, and it is decided by where code is written (lexical scope), not where a function is called from. An inner variable with the same name shadows the outer one.

JS Lexical scope and shadowing

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does for (var i = 0; i < 3; i++) setTimeout(() => console.log(i), 0) print?

    var has one binding shared by all callbacks, and the loop has finished (i is 3) before any callback runs. With let it would print 0 1 2.
  2. 2. What happens when you read a let variable before its declaration?

    let and const exist from the start of the block but cannot be accessed until initialized.
  3. 3. Which statement about const is correct?

    const fixes the binding only. Use Object.freeze for shallow immutability.
  4. 4. What is a closure?

    The function retains access to its creation scope even after the outer function has returned.
  5. 5. What decides which variables a function can see?

    JavaScript uses lexical scoping, so the scope chain follows the structure of the source code.

Summary

  • var is function-scoped and hoisted as undefined; let and const are block-scoped with a temporal dead zone.
  • Hoisting means names are known in advance; nothing physically moves.
  • A closure keeps its creation scope alive and shares variables, not copies.
  • The loop puzzle: var shares one binding; let creates one per iteration.
  • The module pattern (or ES modules and #private fields) gives private state.
  • Scope is lexical: decided by where code is written.

Next: JavaScript: this, Prototypes and Classes