Immediately invoked function expression
An immediately invoked function expression is a JavaScript function expression that is defined and executed immediately within the same syntactic construct, often creating an isolated scope.
Secrets of the JavaScript Ninja, Second Edition
A function expression that is defined and invoked immediately in the same syntactic construct.
- How it is used
- Uses immediate invocation to run one-time work and give pre-ES6 modules a scope for hidden variables.
- How deep it goes
- Explains call-expression syntax, then works through scoped state, closures, and an object-based module interface.
- Examples
- Shows immediate-call syntax, a click-counting module, and a DOM dimension utility that hides temporary style settings.
- What you can do after
- Build it from scratch to run setup immediately while keeping state private and exposing selected functionality.
Functional Programming in JavaScript
An immediately invoked function expression is a function expression that is executed as soon as it is defined, typically to create an isolated scope.
- How it is used
- Uses immediate execution to create module boundaries that hide implementation details and limit globally shared data.
- How deep it goes
- Explains function scope, closures, private variables, and exported methods, but defers detailed variations of module patterns.
- Examples
- Demonstrates a MyModule skeleton whose private variables support exported methods through a closure.
- What you can do after
- Apply it via JavaScript function expressions to encapsulate module state and expose selected functionality.
Read how Functional Programming in JavaScript explains immediately invoked function expression →
The Joy of JavaScript
A function expression that is defined and executed immediately, often to create a private scope and avoid leaking bindings.
- How it is used
- Provides encapsulated namespaces, legacy modularity, and immediate startup execution without requiring a module system.
- How deep it goes
- Explains function-scope encapsulation, namespace exposure, mixin context, and async invocation, while deferring detailed module internals.
- Examples
- Uses a Transaction namespace, a HasHash mixin, and dynamic-import initialization as concrete applications.
- What you can do after
- Build encapsulated namespaces and mixins, or start asynchronous initialization through an immediately invoked async function.
Read how The Joy of JavaScript explains immediately invoked function expression →
Related concepts
- function expressionkind of · A function expression is an expression that creates a function value, which can be assigned, passed, or used within another expression.
- module patternused by · The module pattern is a software design pattern that encapsulates implementation details and state while exposing a controlled public interface.
- scopeproduces · Scope is the region of a program in which names are visible and can be resolved according to the language’s lookup rules.
- modulealternative to · A module is a distinct, bounded software unit that groups related code or functionality and provides an organizational or interaction boundary within a larger system.
- modularityused by · Modularity is the property of a system being organized into separable components with distinct responsibilities and defined interactions.
- encapsulationaddresses · Encapsulation is a software design principle that contains state and implementation details behind a controlled interface, limiting direct access from outside.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


