Skip to content
JavaScript·Intermediate·Coding·1 min read

What problem does the module pattern solve?

Short interview answer

It creates a private, encapsulated scope for implementation details, exposing only a deliberately chosen public API. Consumers can't reach into or accidentally mutate internal state directly, which ES modules now provide natively at the file level.

Example

JavaScript
const counter = (() => {  let count = 0;                 // private  return {    increment: () => ++count,    value: () => count,  };})();
counter.increment(); // 1counter.count;       // undefined

Key takeaway

Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.

← Back to JavaScript design patterns

Related questions