An IIFE, short for immediately invoked function expression, is a function that is defined and called in the same statement: (function () { /* code */ })();. Its purpose is to run code once inside a private scope, so the variables it declares do not leak into the surrounding scope. A javascript IIFE was the standard way to create modules before ES2015, and it still has a few uses today.
This guide shows the syntax variants, what problems an IIFE solves, and what replaced it in modern code.
A normal function declaration does nothing until you call it. An IIFE wraps a function expression in parentheses and calls it at once.
The first pair of parentheses turns the function keyword into an expression. Without them, the parser sees a function declaration and function () {}() fails with a SyntaxError, because a declaration needs a name and cannot be called directly. The second pair of parentheses () calls the function. You will also see the call placed inside the wrapper: (function () {}());. Both forms are valid.
Arrow functions work as well, and so do async ones.
The async form was popular for using await at the top level of old scripts. Today, ES modules support top-level await, so that use is mostly gone in module code.
An IIFE can take parameters, and giving the function a name helps stack traces.
Before let, const and modules, var was function scoped and top-level code shared one global scope. Two scripts using the same variable name would overwrite each other. An IIFE created a function scope to hold the variables.
This is the module pattern. The returned methods are closures over count, so only they can change it. The outside code has no way to read the variable directly.
An IIFE can also run initialisation once, avoid naming a throwaway helper, or compute a value that needs several statements:
const a = 1\n(function () {})() the engine reads 1(function...) and throws a TypeError. End the previous statement with a semicolon, or start the IIFE line with one.this inside a regular IIFE gives undefined in strict mode and the global object in sloppy mode, not the surrounding this. Use an arrow IIFE to keep the outer this.return inside an IIFE returns from the IIFE only, not from the outer function..catch() or a try/catch inside it.Most reasons for an IIFE now have a simpler tool.
| Need | Old approach | Modern approach |
|---|---|---|
| Keep variables private to a file | IIFE module pattern | ES modules with import and export |
| Temporary local variables | IIFE | A block { ... } with let or const |
| Private state in an object | Closure in an IIFE | Class private fields such as #count |
await at the top level | Async IIFE | Top-level await in modules |
IIFEs remain useful in classic scripts you cannot convert to modules, in small inline snippets in HTML, and in bundler output, where tools wrap each file in a function for the same isolation.
They make the parser treat function as an expression instead of a declaration. Only an expression can be called immediately.
No. An IIFE is a way of running a function right away. A closure is a function that remembers variables from its outer scope. They are often combined, as in the module pattern.
Rarely in new code. Use ES modules and block scope first. Keep an IIFE for classic scripts or a one-off computed value.
An anonymous IIFE cannot be called again. A named one can refer to itself by name inside its body, but not from outside.
An IIFE defines a function expression and calls it at once to create a private scope. It powered the module pattern in the era of var and global scripts. Today, ES modules, block scope with let and const, and class private fields cover most of its uses, though it is still valid for classic scripts and inline initialisation.