Movatterモバイル変換


[0]ホーム

URL:


  1. Glossary
  2. IIFE

IIFE

AnIIFE (Immediately Invoked Function Expression) is an idiom in which aJavaScriptfunction runs as soon as it is defined. It is also known as aself-executing anonymous function. The name IIFE is promoted by Ben Alman inhis blog.

js
// standard IIFE(function () {  // statements…})();// arrow function variant(() => {  // statements…})();// async IIFE(async () => {  // statements…})();

It contains two major parts:

  1. Afunctionexpression. This usually needs to beenclosed in parentheses in order to be parsed correctly.
  2. Immediatelycalling 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, thecomma 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 newscope.
  • Creating a new async context to useawait in a non-async context.
  • Computing values with complex logic, such as using multiple statements as a single expression.

For code examples, see thefunction expression andasync function expression reference pages.

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp