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.
0 of 10 checks passed
Your progress is saved on this device.