What is the (function() { } )() construct in JavaScript? Last Updated : 22 Dec, 2023 Comments Improve Suggest changes Like Article Like Report If you've ever played around with JavaScript, you might have seen this expression. It's like a strange set of symbols, but it has a special name that is an immediately invoked function expression, or IIFE. In this article, we will understand each element of the expression as well as the functionalities with the help of examples. Let's start with the detailed definition of the expression to understand its functionalities. (function() { } )() Construct This expression is immediately invoked function expression. It has two parts - function declaration (function() {}) and invocation (). This (function() {}) defines an anonymous function. This is enclosed by () indicates the function() {} is an expression not just a declaration. The () immediately invokes the function. Although the (function() { } )() may appear to be an odd combination of curly braces and parenthesis, it has a specific function in JavaScript. Syntax:(function() { } )() Parameters: (function() { } ): An anonymous function expression is denoted by the function() { } enclosed in parenthesis. (): The function's expression is invoked or called right away by the following pair of parentheses(), which are outside the first set. Example 1: In this example, this expression creates a local scope, preventing the counter variable from polluting the global scope. This encapsulation is beneficial, especially in larger applications where avoiding global variable conflicts is crucial. JavaScript (function () { let counter = 0; function increaseCounter() { counter++; } increaseCounter(); console.log(counter); })(); Output1 Example 2: Here, the IIFE is used to create a module with private and public members. The private members (privateVariable and privateFunction) are encapsulated within the IIFE, making them inaccessible from outside the module. The returned object contains only the public members. JavaScript let Module = (function () { let privateVariable = "I am private."; function privateFunction() { console.log("This is a private function."); } return { publicVariable: "I am public.", publicFunction: function () { console.log("This is a public function."); } }; })(); console.log(Module.publicVariable); Module.publicFunction(); console.log(Module.privateVariable); // Module.privateFunction(); // Error: Module.privateFunction is not a function OutputI am public. This is a public function. undefined Conclusion: JavaScript's (function() { } )() construct may look confusing at first, but it's a very useful tool for writing code blocks that are instantly executed and self-contained. You can prevent contaminating the global namespace and run code exactly when required by enclosing it inside an IIFE. We can write more organized and modular JavaScript code by using the IIFE. Comment More infoAdvertise with us Next Article What is the (function() { } )() construct in JavaScript? harishcarpenter Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League Geeks Premier League 2023 Similar Reads What is the first class function in JavaScript ? First-Class FunctionA programming language is said to have First-class functions if functions in that language are treated like other variables. So the functions can be assigned to any other variable or passed as an argument or can be returned by another function. JavaScript treats function as a fir 2 min read What is a Constructor in JavaScript? A constructor in JavaScript is a special function that is used to create and initialize objects. When we want to create multiple objects with similar properties and methods, the constructor is used as a blueprint to create similar objects. This is useful when you want to create multiple objects with 8 min read What is the inline function in JavaScript ? In JavaScript, an inline function is a special type of anonymous function that is assigned to a variable, or in other words, an anonymous function with a name. JavaScript does not support the traditional concept of inline functions like C or C++. Thus anonymous function and inline function is practi 2 min read What is Currying Function in JavaScript? Currying is used in JavaScript to break down complex function calls into smaller, more manageable steps. It transforms a function with multiple arguments into a series of functions, each taking a single argument.It converts a function with multiple parameters into a sequence of functions.Each functi 3 min read JavaScript Function() Constructor The JavaScript Function() constructor is used to create new function objects dynamically. By using the Function() constructor with the new operator, developers can define functions on the fly, passing the function body as a string. This allows for greater flexibility in situations where functions ne 2 min read What is the syntax for leading bang! in JavaScript function ? Before we get to know the syntax for the leading bang! in a JavaScript function, let's see what functions in JavaScript are actually are. JavaScript functions are a set of statements(procedures) that perform some tasks or calculate some value. A function may take some input and return the result to 2 min read What is the usage of Function.prototype.bind in JavaScript ? Bind method: Using this method, we can bind an object to a common function, so that gives different result when its needed. The bind() method takes an object as an argument and creates a new function. So basically bind function return function. Let's understand when bind method is used. bind the obj 2 min read What is a typical use case for anonymous functions in JavaScript ? In this article, we will try to understand what exactly an Anonymous function is, and how we could declare it using the syntax provided in JavaScript further we will see some examples (use-cases) where we can use anonymous functions to get our results in the console. Before proceeding with the examp 4 min read Using the function* Declaration in JavaScript The function* declaration is used to define a generator that returns a Generator object. Generators are very powerful for asynchronous programming as they aim to resolve callback problems. In a generator, the yield keyword is used instead of return. The yield statement suspends the functionâs execut 2 min read JavaScript - What is the Purpose of Self Executing Function? The self-executing anonymous function is a special function which is invoked right after it is defined. There is no need to call this function anywhere in the script. This type of function has no name and hence it is called an anonymous function. The function has a trailing set of parentheses. The p 2 min read Like