IIFEs In JavaScript Explained

IIFEs in Javascrpt are simply functions that are called or invoked immediately they are declared.

The Use of IIFEs in JavaScript is one of the updates EcmaScript 2015 (popularly called ES6) brought about.

IIFEs is an abbreviation for Immediately Invoked Function Expressions. Quite Explicit I say :)

Before ES6, the traditional way for invoking functions immediately they are declared usually goes thus:

As we can see that works just as fine as we expected.

With ES6 however, the implementation is quite similar.

So here’s how its done:

1. Before the declaration of the function, an opening parenthesis is placed

2. Immediately after the closing curly brace of the function, the parenthesis we put at the beginning of the function declaration is closed. In other words, the entire function declaration is enclosed in parenthesis.

3. Once the function has been enclosed in parenthesis as we have done in step 2, another pair of curly braces is placed immediately.

4. We can then end our statement with a semi-colon.

On running our code, it works just the same as the first example we ran.

That’s it about Immediately Invoked Function Expressions aka IIFEs folks!

I”d Like to hear your opinion about this feature of ES6. Do you feel its welcome or is it just another complexity in JavaScript ES6 brought about.

Personally, I haven’t gotten used to the use of IIFEs as I only implement them in my code once in a while. I feel it reduces code readability as do Arrow Functions.

Over to you now, IIFEs Yay or Nay?

More From Medium

Also tagged JavaScript

Top on Medium

Ed Yong
Mar 25 · 22 min read


Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade