What is an IIFE in JavaScript?
Learn what an IIFE (Immediately Invoked Function Expression) is in JavaScript, its syntax, private scope, the module pattern, and modern alternatives.
Expected Interview Answer
An IIFE (Immediately Invoked Function Expression) is a function that is defined and executed right away in a single expression, typically to create a private scope and avoid polluting the global namespace.
It is written by wrapping a function in parentheses to turn it into an expression and then calling it immediately with a trailing pair of parentheses. Any variables declared inside stay private to the function's scope, which historically was the main way to achieve encapsulation and the module pattern before ES modules and block-scoped let and const existed.
- Creates a private scope for variables
- Avoids polluting the global namespace
- Runs setup code exactly once
- Underpins the classic module pattern
- Prevents naming collisions between scripts
AI Mentor Explanation
An IIFE is like a one-off practice drill set up, run once before the match, and packed away immediately. The cones and nets used exist only for that drill and never clutter the actual pitch, just as an IIFE's variables live only inside it and never reach the outer game.
Step-by-Step Explanation
Step 1
Write a function
Start with a normal function containing the code you want to run once.
Step 2
Wrap it in parentheses
Surround the function with parentheses so the parser treats it as an expression, not a declaration.
Step 3
Invoke immediately
Add a trailing pair of parentheses to call the function the moment it is defined.
Step 4
Keep variables private
Declare variables inside so they stay scoped to the function and never leak out.
Step 5
Optionally return a value
Return an object or value to expose a controlled public interface, as in the module pattern.
What Interviewer Expects
- The expansion Immediately Invoked Function Expression
- Correct syntax with wrapping and trailing parentheses
- Understanding that it creates a private scope
- Awareness of its role in the module pattern
- Knowing modern alternatives like block scope and ES modules
Common Mistakes
- Forgetting the wrapping parentheses so it parses as a declaration
- Thinking an IIFE must be anonymous when it can be named
- Confusing an IIFE with a regular function that is merely called later
- Claiming IIFEs are obsolete rather than largely superseded by modules
- Missing a required semicolon before an IIFE, causing ASI issues
Best Answer (HR Friendly)
“An IIFE is a function that runs itself the instant it is written. Developers use it to keep some variables private and separate from the rest of the code, so different scripts do not accidentally clash with each other.”
Code Example
(function () {
const secret = 'hidden';
console.log('runs immediately:', secret);
})();
// Arrow function form
(() => {
console.log('also runs immediately');
})();const counter = (function () {
let count = 0; // private state
return {
increment() { count++; return count; },
reset() { count = 0; },
};
})();
console.log(counter.increment()); // 1
console.log(counter.increment()); // 2
counter.reset();
console.log(counter.increment()); // 1
// count is not accessible directly from outsideFollow-up Questions
- How does an IIFE create a private scope?
- How did IIFEs implement the module pattern before ES modules?
- What are the differences between the parenthesized and arrow IIFE forms?
- Why might you need a leading semicolon before an IIFE?
- How have let, const, and ES modules reduced the need for IIFEs?
MCQ Practice
1. What does IIFE stand for?
IIFE stands for Immediately Invoked Function Expression — a function defined and called at once.
2. What is the primary purpose of an IIFE?
Wrapping code in an IIFE keeps its variables local, preventing collisions with the global scope.
3. Which syntax correctly forms an IIFE?
Wrapping the function in parentheses makes it an expression, and the trailing () invokes it immediately.
Flash Cards
What does IIFE stand for? — Immediately Invoked Function Expression.
Why use an IIFE? — To create a private scope and avoid polluting the global namespace.
What is the basic IIFE syntax? — Wrap a function in parentheses and add a trailing () to call it at once: (function(){})().
What modern features reduce the need for IIFEs? — Block-scoped let and const, plus ES modules, which provide isolation natively.