MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Immediately Invoked Functions (IIFE)0%
Study Materials
JavaScript for IntermediateProgress 0%
1.1Block Scope with let & const1.2Template Literals1.3Destructuring Arrays and Objects1.4Default Parameters1.5Enhanced Object Literals
2.1Arrow Functions Advanced2.2Rest & Spread Operators2.3Closures Introduction2.4Immediately Invoked Functions (IIFE)
HomeTutorialsJavaScript for IntermediateImmediately Invoked Functions (IIFE)
querySelectorAll & forEach

Immediately Invoked Functions (IIFE)

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

Immediately Invoked Function Expressions (IIFE)

An IIFE (pronounced "iffy", Immediately Invoked Function Expression) is a JavaScript design pattern where a function runs immediately as soon as it is defined.


1. Syntax of an IIFE

An IIFE is composed of two primary parts:

  1. 1
    The Grouping Operator (...): Wraps the function declaration, instructing the JavaScript parser to treat it as an expression rather than a statement.
  2. 2
    The Invocation Operator (): Immediately calls the function expression.
1(function() {
2 console.log("I run immediately upon declaration!");
3})();

Modern Arrow Function IIFE Syntax:

1(() => {
2 console.log("Arrow function IIFE executed!");
3})();

2. Why Did Developers Use IIFEs? (Legacy Context)

Before ES6 introduced let, const, and ES Modules, all variables declared with var were globally scoped unless inside a function. If you included multiple script files on a web page:

1// file1.js
2var count = 0;
3 
4// file2.js
5var count = 100; // 💥 OOPS! file2 silently overwritten file1's global variable!

The Solution: The IIFE Sandbox Pattern

By wrapping each file inside an IIFE, developers created a private function scope, completely protecting the global window namespace from variable collisions:

1// file1.js
2(function() {
3 var count = 0; // Private! Cannot collide with file2!
4})();
5 
6// file2.js
7(function() {
8 var count = 100; // Private!
9})();

3. Modern Use Cases for IIFEs

While ES Modules (import/export) have largely superseded IIFEs for namespace isolation, IIFEs are still widely used in modern JavaScript for:

A. Top-Level async Execution in Older Environments:

1(async () => {
2 try {
3 const res = await fetch("https://api.github.com/users/sumit-msk");
4 const data = await res.json();
5 console.log("User:", data.name);
6 } catch (err) {
7 console.error("Fetch failed:", err);
8 }
9})();

B. Creating Module Singletons:

1const DatabaseConnection = (() => {
2 let instance = null;
3 
4 function createInstance() {
5 return { connectionId: Math.random() };
6 }
7 
8 return {
9 getInstance() {
10 if (!instance) instance = createInstance();
11 return instance;
12 }
13 };
14})();
15 
16console.log(DatabaseConnection.getInstance().connectionId);

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What does IIFE stand for?
Question 2
What is the purpose of wrapping the function in parentheses (function() { ... })?
Question 3
Historically, what was the primary motivation for using the IIFE pattern?
Question 4
How is an arrow function written as an IIFE?
Question 5
Can arguments be passed into an IIFE?
Next Lesson

querySelectorAll & forEach

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Closures IntroductionquerySelectorAll & forEach

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Intermediate with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext