JavaScript
/Intermediate
IIFE (Immediately Invoked Function Expression)
Definition
A JavaScript function that runs as soon as it is defined. It is wrapped in parentheses to make it an expression, followed by `()` to invoke it immediately.
Explain Like I'm New
An IIFE is a disposable function. You write it, it runs instantly, it creates a private sandbox for variables so they don't leak into the global scope, and then it is immediately forgotten.
Real World Example
Before ES6 modules existed, developers used IIFEs to bundle libraries like jQuery, ensuring that their internal variables didn't accidentally overwrite the user's variables.
Common Use Cases
- •Data privacy
- •Avoiding global namespace pollution
- •Top-level await alternatives in older JS
Interactive Example
Loading...
Console output will appear here...
Interview Questions
basic
- What does IIFE stand for?
intermediate
- Why are the outer parentheses necessary?
advanced
- Are IIFEs still necessary in modern JavaScript (ES6+)?