liveBook · Manning

Immediately invoked function expression

As explained in 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.

An immediately invoked function expression (IIFE) is a function expression that runs as soon as it is defined. Its function scope can hide internal variables while exposing only selected functionality. section 2.4

Used with closures, an IIFE can form the private boundary of a module: internal methods and data stay inaccessible, while exported methods provide the outside interface. This reduces globally shared data and references. section 2.4

A module boundary

A global MyModule object is passed into a function expression that executes when the script loads. Private variables such as _myPrivateVar remain local to the wrapping function, while exported methods use a closure to access the module’s internal state. section 2.4

An IIFE supports:

  • Encapsulating internal variables and methods. section 2.4
  • Exposing a deliberately limited set of functionality. section 2.4
  • Packaging functional code inside well-encapsulated modules. section 2.4

How Functional Programming in JavaScript covers it

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 it assumes
Builds on closures and function scope while using them to explain private module variables and exported behavior.
What you can do after
Apply it via JavaScript function expressions to encapsulate module state and expose selected functionality.
How it connects
Connects IIFEs with closures, function scope, encapsulation, modules, and global namespace management.

Also explained in

Compare how 3 books explain immediately invoked function expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.