The Module Pattern (IIFE)
Encapsulate code with immediately invoked functions.
The Module Pattern (IIFE) is a free JavaScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What the Module Pattern Solves
Before ES modules, the module pattern let you bundle related state and behavior while keeping internals private and exposing a clean public API. It is built on closures.
The IIFE
An IIFE (Immediately Invoked Function Expression) is a function that runs the moment it is defined. Wrapping in parentheses and calling it creates a private scope instantly.
const result = (function () {
return 42;
})();
console.log(result); // 42Why Run It Immediately
The IIFE gives you a one-time private scope without leaving a named function lying around. Everything inside is isolated from the global namespace.
(function () {
const temp = "not global";
console.log(temp);
})();
// temp is unreachable out hereReturning a Public API
The module pattern is an IIFE that returns an object. The object methods are closures over the private variables, forming the public interface.
const counter = (function () {
let count = 0; // private
return {
inc: () => ++count,
value: () => count
};
})();
counter.inc();
console.log(counter.value()); // 1Private vs Public
Anything declared inside but not returned stays private. Only what you put on the returned object is accessible.
const calc = (function () {
const pi = 3.14159; // private constant
return {
area: (r) => pi * r * r
};
})();
console.log(calc.area(2)); // 12.56636
console.log(calc.pi); // undefinedPrivate Helpers
Helper functions can live inside the module, used by public methods but never exposed.
const validator = (function () {
const clean = (s) => s.trim().toLowerCase(); // private
return {
isEmail: (s) => clean(s).includes("@")
};
})();
console.log(validator.isEmail(" A@B.com ")); // trueThe Revealing Module Pattern
A popular variant defines all functions privately, then returns an object that "reveals" references to the public ones. It keeps the public API list clear at the bottom.
const store = (function () {
let items = [];
function add(x) { items.push(x); }
function count() { return items.length; }
return { add, count }; // reveal
})();
store.add("a");
console.log(store.count()); // 1Singletons
Because the IIFE runs once, a module is effectively a singleton: there is exactly one instance with one set of private state.
const config = (function () {
let settings = { theme: "dark" };
return {
get: (k) => settings[k],
set: (k, v) => { settings[k] = v; }
};
})();
config.set("theme", "light");
console.log(config.get("theme")); // "light"Passing in Dependencies
You can pass globals or other modules as IIFE arguments, making dependencies explicit and enabling safe minification.
const mathUtils = (function (M) {
return { roundUp: (n) => M.ceil(n) };
})(Math);
console.log(mathUtils.roundUp(4.1)); // 5Avoiding Global Pollution
The whole point is one global name (the module) instead of dozens of loose variables. This prevents naming collisions across scripts.
Relation to ES Modules
Today, import/export give true module scope at the language level, largely replacing this pattern. But understanding IIFE modules clarifies how privacy and encapsulation work, and you will still meet them in legacy code.
Quick Check
The module pattern and IIFEs.
Recap
The module pattern uses an IIFE to create a private scope, returning an object whose methods are closures over hidden state. Internals stay private; only the returned members are public. Variants like the revealing module pattern clarify the API, and modules act as singletons. ES import/export now supersede the pattern but build on the same ideas.
Frequently asked questions
Is the “The Module Pattern (IIFE)” lesson free?
Yes — the full text of “The Module Pattern (IIFE)” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “The Module Pattern (IIFE)”?
Encapsulate code with immediately invoked functions. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The Module Pattern (IIFE)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- How Closures Capture State
- Private Variables with Closures
- The Module Pattern (IIFE)
- Factory Functions