IIFE (Immediately Invoked Function Expressions)
Last Updated: 22nd October 2025
IIFE is a function that runs immediately after it is defined. It does not need to be called separately.
Syntax:
(function () {
// code here
})();
// Or
(() => {
// code here
})();
Hinglish Tip 🗣: IIFE ka matlab hai “define karte hi run ho jao”. Ye mostly scope isolation ke liye use hota hai.
Example 1:
(function () {
let msg = "Hello from IIFE!";
console.log(msg);
})();
Example 2:
(function (name) {
console.log("Hello " + name);
})("Sadhu");
Example 3:
(() => {
console.log("I am an arrow IIFE!");
})();
💡 Quick Practice
- Create an IIFE that adds two numbers and logs the result.
- Try defining variables inside IIFE and access them outside (you should get an error).
- Convert a normal function into an IIFE.