DEV Community

Cover image for Immediately Invoked Function Expression
Antony Kimani
Antony Kimani

Posted on

Immediately Invoked Function Expression

I was struggling to understand IIFE's so I wrote about it. Hopefully this article helps you understand IIFE's in a Jiffy.

Immediately Invoked Functions are Functions that execute as soon as it is defined.

Here is an Example:

(function(){
    console.log('This is an Immediately Invoked Function');
})();
Enter fullscreen mode Exit fullscreen mode

It has two enclosing parenthesis , the First parenthesis makes the function an expression and the last parenthesis tells the JavaScript compiler to invoke or call the function Immediately.

The Expression evaluates itself to execute the function.

Is that it?

what are the uses of IIFE?

1. Avoid Calling Variables in the global scope and create closures

With IIFE being executed immediately it means variable names will not conflict.This is better in situations where we don't need to use the function again.

However, with the introduction of let and const variable keywords we can differentiate between global and block scope variables.

Hope this explained the concept in an easy , beginner-friendly manner.Cheers

Top comments (0)