DEV Community

Discussion on: Explain IIFE(Immediately Invoke Function Expression) Like I'm Five

owenconti profile image
Owen Conti 🇨🇦 • Edited on


An item you put on your to-do list and then do right away.

Normal function:

An item you put on your to-do list and you'll get to it later.

An IIFE is nothing more than a function that is called once and it's called right away.

They used to be big back in the day to avoid variable scoping issues:

// app.js

var secret = '123';

The above secret variable is in the window scope, so it is accessible anywhere in the runtime. Changing to use an IIFE, it put the variable in the function's scope, meaning it cannot be accessed outside the function:

// app.js

(function() {
   var secret = '123';

// I can't see `secret` down here!!


latheresienne profile image
Latheresienne Author

Thanks for the explanation. Your approach with a to-do list is just great!