An IIFE (Immediately Invoked Function Expression) is a JavaScript function that runs as soon as it is defined.

It is a design pattern which is also known as a Self-Executing Anonymous Function and contains two major parts:

The first is the anonymous function with the lexical scope enclosed within the Grouping Operator (). This prevents accessing variables within the IIFE idiom as well as polluting the global scope.

The second part creates the immediately invoked function expression () through which the JavaScript engine will directly interpret the function.

For example,

(function () {
    console.log("This is Immediately Invoked Function");
 })();

The above function will automatically run when you open a browser or refresh the browser.

Pass Argument in IIFE:

(function (name) {
    console.log('This is Immediately Invoked Function,Created by '+name );
 })('Jigar');
//Output : This is Immediately Invoked Function,Created by Jigar

Use cases :

When you want your data ta be private

Want to call the function on page load

Topics covered:

Found this article helpful?

TutsCoder tutorials are free and ad-light — supported by readers like you. Buy me a coffee (or two ☕☕) as a token of appreciation and help keep Angular & Node.js content coming!

One-time. No subscription. 100% optional. 🙏 Every coffee counts!

Leave a Comment

Your email will not be published. Spam-free zone. ✌️

Available for Projects

Need Help With Your
Angular or Node.js Project?

7+ years of MEAN Stack experience. I build scalable Angular 21 apps, Node.js APIs, and SaaS products — delivered on time, every time.

7+ Years MEAN Stack Angular 21 + Nx Expert 20+ Projects Delivered Remote / Freelance