An IIFE (Immediately Invoked Function Expression) is an idiom in which a JavaScript function runs as soon as it is defined. It is also known as a self-executing anonymous function. The name IIFE is promoted by Ben Alman in his blog.
// standard IIFE
(function () {
// statementsâ¦
})();
// arrow function variant
(() => {
// statementsâ¦
})();
// async IIFE
(async () => {
// statementsâ¦
})();
It contains two major parts:
IIFEs are a common pattern used to execute arbitrarily many statements in their own scope (and possibly return a value), in a location that requires a single expression. They are similar to, but much more powerful than, the comma operator, which can only execute multiple expressions and, therefore, does not provide a way to use local variables or control flow statements.
Use cases of IIFEs include:
await
in a non-async context.For code examples, see the function
expression and async function
expression reference pages.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4