Skip to content

Chapter 11 of 33

Scope and Closures

Reason about lexical scope, hoisting, TDZ, closures, privacy, and retained state.

40 minutes 10 quick checksBy Subha Prasad
Lesson 11 of 33Course navigation

Lesson content

Read, practise, then check your understanding

JavaScript resolves names lexically from source nesting. let/const have block scope and a temporal dead zone; var has function scope and is initialized to undefined during hoisting.

Private retained state

function createCounter() {
  let value = 0;
  return {
    increment() { value += 1; },
    read() { return value; }
  };
}

const counter = createCounter();

The returned methods close over value after createCounter returns. Closures power callbacks, factories, and modules but can retain large objects or DOM nodes longer than intended. Avoid accidental shadowing, declare bindings near use, and release listeners/timers that keep closure graphs alive.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes lexical scope?
Which JavaScript term matches this description: Name resolution determined by source-code nesting.
Which statement best describes closure?
Which JavaScript term matches this description: A function together with access to its surrounding lexical environment.
Which statement best describes temporal dead zone?
Which JavaScript term matches this description: The region where a lexical binding exists but cannot yet be accessed.
Which statement best describes shadowing?
Which JavaScript term matches this description: An inner binding hiding an outer binding with the same name.
Which statement best describes module scope?
Which JavaScript term matches this description: Top-level isolation provided by an ECMAScript module.

0 of 10 checks passed

Your progress is saved on this device.

Scope and Closures | JavaScript Lesson | Subha Prasad