DEV Community

Randy Rivera
Randy Rivera

Posted on

Understanding the Immediately Invoked Function Expression (IIFE)

  • A common pattern in JavaScript is to execute a function as soon as it is declared:
(function () {
  console.log("Chirp, chirp!");
})();
Enter fullscreen mode Exit fullscreen mode
  • This is an anonymous function expression that executes right away, and outputs Chirp, chirp! immediately.
  • Note that the function has no name and is not stored in a variable. The two parentheses () at the end of the function expression cause it to be immediately executed or invoked. This pattern is known as an immediately invoked function expression or IIFE.

  • Let's rewrite the function makeNest and remove its call so instead it's an anonymous immediately invoked function expression (IIFE).

function makeNest() {
  console.log("A cozy nest is ready");
}

makeNest();
Enter fullscreen mode Exit fullscreen mode
  • Answer:
(function () {
  console.log("A cozy nest is ready");
})();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)