What is IIFE in JavaScript?

gyi2521 on September 15, 2018

The first time I heard 'IIFE' in my Coding Bootcamp class, it immediately reminded me of my sister's dog 'Yeffi' which means 'pretty' in some hum... [Read Full]
markdown guide
 

Great post, clear and to the point. Thanks for sharing!

 

Nice description. Thank you!

Although, I do not know at the moment whether the term closure might help in this description (it explains, how privacy is achieved) or if it would confuse people (because it is really advanced) reading this.

Perhaps you may consider it to mention in a side note ;)

 

Most people might not know this but if you are accustomed to not using semicolons you will run into cryptic runtime errors when using an IIFE. The solution is to always put a semicolon before an IIFE ;(() => {})()

 

I think semicolon is voluntary or best practice reason is if statement was not completed from previous code or semicolon do the job so we worry about our function only.

 

Super well described.

Anybody know if this behavior has any parallels in other languages?

 

It all boils down to scopes. JavaScript is weird, well actually ES5 is weird. Back then with the var keyword, variables we're limited by function scope. Meaning, only functions could create scopes, unlike normal languages that have block scopes (i.e. a for loop creates a separate scope).

Nowadays with const and let block scopes are added in ES6 and above. Hence, making IIFEs less prominent as of late.

To get back to the question. Every normal language that has block scopes has this behavior. 😄

 
 

Another very common reason to use this is with async/await, since await doesn't work globally, only under an async function.

 

Great content this how JQuery library was built hiding its properties from GLOBAL EXECUTION CONTEXT allowing compiler to access its object.

 
code of conduct - report abuse