Closures and IIFEs show up constantly in JavaScript interviews — and in real codebases.
Closures
A closure is created when a function remembers the variables from the scope where it was defined, even after that outer function has finished running.
That is the foundation of many patterns: private state, factories, event handlers, and functional composition.
Eric Elliott’s writing on closures is a solid deep dive if you want more use cases beyond the textbook counter example.
IIFEs
An Immediately Invoked Function Expression runs as soon as it is defined:
(function () {
const secret = 42;
// secret stays out of the global object
})();
IIFEs were especially important before ES modules were widespread. They still help when you need a quick isolated scope that does not pollute globalThis / window.
Master both, and a large chunk of “tricky” JS interview questions become straightforward.