Posted in Interesting, Programming, Web Tech

IIFE : Immediately Invoked Function Expression

What is IIFE?

As the name says, IIFE, Immediately Invoked Function Expression is a function which is immediately invoked as soon as it is defined in javascript programming. It is a very popular way of handling javascript programs where the interpreter runs it only once.

Different syntax:

  1. ( function( ){ //statements })( );
  2. ( function( ){ //statements }( ));
  3. ( ( )=>{ //statements })( );

It has two parts: One is the function enclosed within () group operator, helps in accessing variables within function and second part which directly creates the immediately invoked function expression () which the javascript engine interprets. This whole concept of design pattern is called as Self Executing Anonymous Function.

As the function name is not used, they are called as anonymous function.

When is IIFE important?

As IIFE is executed once it is defined and interpreted once by javascript engine.

It maintains encapsulation and avoids polluting of variables which are not required for global access.

The code which does not have reusability can also be a part of IIFE. Hence this function is also an anonymous function.