Member-only story

An Introduction to IIFEs for Beginners

What are IIFEs? IIFEs or Immediately Invoked Function Expressions are functions that get invoked the moment they are defined.

Abhimanyu Chauhan
JavaScript in Plain English
4 min readJan 26, 2022

--

Immediately Invoked Function Expression

Immediately Invoked Function Expression, you are using them without knowing

IIFE are what you call Immediately Invoked Function Expressions, is a function that gets invoked the moment is defined. It’s also a pattern known as the Self-Executing Anonymous Function. A basic IIFE looks like

(function (){
console.log("You Don't Invoke Me, I Invoke Me");
})()

The moment the function is read it gets invoked:

At a glance, it looks simple, and tbh it is. Where things get a little complicated is deciding where to use them. So let’s look at some examples and use cases to understand how they work, and the final result might be a little more surprising than you would expect.

Let’s keep the Global Namespace clean

With modern JavaScript applications, we are injecting a lot of dependencies, and some of them are either dependencies that get defined in the global/window object. But what if we define a global variable that overrides a variable, used by another dependency, things will break. There is a reason why…

--

--

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Written by Abhimanyu Chauhan

JS Enthusiast, with a passion for travelling and writing.

No responses yet

Write a response