← Node.js Foundations
14 / 25
Lesson 14 — Modules pattern
Hide the inside, share the outside
A good module exposes a small API (the functions others may call) and hides its internals. A closure does this: variables declared inside a function stay private, but functions returned from it can still use them.
const calc = (() => { let s = 0; return { add: (x) => { s += x; } }; })();
Nobody outside can touch s directly.
Why it matters: private state cannot be broken by accident from another file.
Want it another way?
Task
Add a reset() method that sets the private sum back to 0, then check total after reset.
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.