Immediately Invoked Function Expression (IIFE)

Immediately Invoked Function Expression (IIFE)

Immediately Invoked Function Expression (IIFE)

An IIFE is a JavaScript function that is executed as soon as it is defined. It can also be referred to as Self-Executing Anonymous Function. It is basically a function that is defined inside a parenthesis then a parenthesis is appended to execute it.

Image for post

An anonymous function with lexical scope enclosed within the parenthesis() — Grouping Operator. This prevents variables within the IIFE from being accessed.

Image for post

The appended parenthesis creates the immediately invoked function expression through which the JavaScript engine will directly interpret the function.

Image for post

IIFEs are very useful and they are a simple way to isolate variables declarations ( variables within the expression can not be accessed from outside it).

Assigning the IIFE to a variable store the function’s return value and not the function definition.

Image for post

I hope this helps someone…