IIFE

Immediately Invoked Function Expression

Stiven Castillo
August 17, 2023

IIFE stands for “Immediately Invoked Function Expression.” It’s a design pattern in JavaScript that involves defining and executing a function immediately after it’s created. IIFE is often used to create a private scope for variables, preventing them from polluting the global scope and helping to manage variable lifetimes.

(function() {
  // code inside this function is executed immediately
  var localVar = "I am a local variable.";
  console.log(localVar);
})();

// localVar is not accessible here, it's confined to the IIFE's scope

IIFEs are often used to encapsulate code and create modular code structures. They were more commonly used before ES6 introduced block-scoped variables