IIFE
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.
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](https://benalman.com/news/2010/11/immediately-invoked-function-expression/#iife).
```js
// standard IIFE
(function () {
// statements…
})();
// arrow function variant
(() => {
// statements…
})();
// async IIFE
(async () => {
// statements…
})();
```
It contains two major parts:
1. A [function _expression_](/docs/Web/JavaScript/Reference/Operators/function). This usually needs to be [enclosed in parentheses](/docs/Web/JavaScript/Reference/Operators/Grouping) in order to be parsed correctly.
2. Immediately _calling_ the function expression. Arguments may be provided, though IIFEs without arguments are more common.
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](/docs/Web/JavaScript/Reference/Operators/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:
- Avoiding polluting the global namespace by creating a new scope.
- Creating a new async context to use await in a non-async context.
- Computing values with complex logic, such as using multiple statements as a single expression.
For code examples, see the [`function` expression](/docs/Web/JavaScript/Reference/Operators/function) and [`async function` expression](/docs/Web/JavaScript/Reference/Operators/async_function) reference pages.
## See also
- [IIFE](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression) (Wikipedia)
- [Comma operator](/docs/Web/JavaScript/Reference/Operators/Comma_operator)
- Related glossary terms:
- Function
Cite this
"IIFE". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Glossary/IIFE JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy