Lesson 2 — Closures & private state
Functions remember
The scope of a variable is the part of the code that can see it. When a function is created inside another function, it keeps access to the outer function's variables even after the outer function has finished. That remembered link is called a closure.
function makeGreeter(word) {
return (name) => word + ", " + name; // still sees word later
}
const hi = makeGreeter("Hi");
console.log(hi("Sam")); // Hi, Sam
Why it matters: closures give you private state that nothing outside can touch — counters, caches and event handlers all rely on them.
Want it another way?
Task
Finish makeCounter so each call to next() returns the next number: 1, 2, 3.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
Test your knowledge
A quick 3-question quiz on this lesson — instant feedback.