Wednesday, November 4, 2015

What is IIFE in JavaScript

IIFE means Immediately Invoked Function Expression.
It is just an anonymous function that is wrapped inside of a set of parentheses and invoked immediately.
We use IIFE to place all our code inside of a local scope or to avoid global scope.

Structure of the IIFE


Example


In the above example, the code outside of the IIFE has a global scope and the code inside of the IIFE has a local scope. Any variables declared inside of an IIFE can not be assessed from the outside of that IIFE.
It is one of the best practice of JavaScript to avoid global variables and functions. There are lots of design patterns in which we can avoid global variables and functions, IIFE is one of them.

Link of program
IIFE Program

No comments:

Post a Comment