IIFE in JavaScript

Immediately Invoked Function Expression (IIFE) is one of the most popular design patterns in JavaScript. As name suggest, IIFE is a function expression that automatically invokes after completion of the definition. The parenthesis () plays important role in IIFE pattern. In JavaScript, parenthesis cannot contain statements; it can only contain an expression. It is a design pattern which is also known as a Self-Executing Anonymous Function and contains two major parts. The first is the anonymous function with lexical scope enclosed within the Grouping Operator () . This prevents accessing variables within the IIFE idiom as well as polluting the global scope. The second part creates the immediately executing function expression () through which the JavaScript engine will directly interpret the function.
(function () { statements })();
The above is called IIFE . You can write all the functions and variables inside IIFE without worrying about polluting the global scope or conflict with other's JavaScript code which have functions or variables with same name. It's just an anonymous function that is executed right after it's created. This pattern is often used when trying to avoid polluting the global namespace, because all the variables used inside the IIFE (like in any other normal function) are not visible outside its scope .