Immediately Invoked Function Expression ES6

Immediately Invoked Function Expression (IIFE)

Immediately Invoked Function Expression (IIFE) is a JavaScript function used to ignore the variable hoisting from inside the code block. An IIFE is a function that runs when we define it. The Immediately Invoked Function Expression allows us to access the method publicly; while it is managing the privacy for variables declared inside a function. The IIEF is also named as Self-Executing Anonymous Functions.

The Immediately Invoked Function Expression includes the following two parts-

  • It is enclosed with a Grouping Operator ().
  • It is used to interpret functions directly by the JavaScript engine.

Syntax

(function ()
 {
        // Block statement
 }) (); 

Example

Here, we have tried to elaborate the concept of IIFE with the help of following example.

(function ()
 {
        console.log (“An Example of IIFE”);
 }) (); 

Output

After the execution of the code, we will get the following output.

Immediately Invoked Function Expression

Function conversion into IIFEs

Some steps are given below that helps us to convert the function into Immediately Invoked Function Expression-

Step 1: First, we take a regular function definition.

Step 2: Then, we bind that definition with the parentheses to create the function expression.

Step 3: In the final step, we associate the expression with the parentheses ({}) and a semicolon (:) as the sign of ending of the statement.

We will try to understand it with the help of the following example-

Example

function alpha ()                                       // Regular Function
 {
      Console.log (“Normal Function”);     
 };
       alpha ();                                                 // Execution of Regular Function
 (function ()                                                 // Declaration and Execution of IIFE
 {                                                                 
                Console.log (“An Immediately Invoked Function Expression”);
 }) ();  

Output

After the execution of the above code, we will get the following output.

Immediately Invoked Function Expression

Some important points about IIFE

Example

(function (p, q, r) 
 {
                console.log (p);
                      console.log (q);
 console.log (r);
 }) (50, 150, 100);  

Output

After the compilation of the code, we will get the following output.

Immediately Invoked Function Expression

Related Topics

Object Destructuring in ES6

Object Destructuring in ES6 The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values...

3 minutes read.

Loops in ES6 | ECMAScript 6

Loops in ECMAScript 6 A Loop statement can be defined as a group of instructions that are always repeated. In the computer programming language, the loops help us to execute a collection of instructions...

8 minutes read.

Animation in ES6

Animation in ES6 In JavaScript, the animation feature is used to manage the aspect that the CSS (Cascading Style Sheet) can’t. JavaScript provides various new elements that help us to construct...

8 minutes read.

Spread Operator in ES6

Spread Operator in ECMAScript 6 The term 'Spread Operator' can be defined as an operator that allows expending the array into separate elements. Three dots (…) isthe syntax of the Spread Operator.The Spread operator...

5 minutes read.

Immediately Invoked Function Expression ES6

Immediately Invoked Function Expression (IIFE) Immediately Invoked Function Expression (IIFE) is a JavaScript function used to ignore the variable hoisting from inside the code block. An IIFE is a function that runs...

3 minutes read.

Array Destructuring in ES6

Array Destructuring in ES6 The term ‘Destructuring’ is defined as dividing the complicated data structure into easier modules. The Destructuring syntax helps to extract the small segments from an array and the objects....

3 minutes read.

Features of ES6 | ECMAScript 6

The ES6 or ECMAScript 2015 came with various exiting and new features. These features make it a more robust and updated programming language than ES5. Some of these new promising features are Const...

6 minutes read.

Array Methods in ES6

Array Methods in ES 6 ES 6 introduced the various new methods such as Array,from(), Array,of(). Here, we have the various array methods used in JavaScript, they are given below in the table...

10 minutes read.

ES6 | ECMAScript 6 Syntax

The term 'syntax' defines the grammar and spelling of any programming language. In Computer science, “Term syntax is a collection of some rules and regulations that determines the group of symbols used in...

8 minutes read.

Dialog box in ES6

Dialog box in ES6 A dialog box can be defined as a regular type of window in the Graphical User Interface (GUI) of the operating system. It is also spelled as...

5 minutes read.

Arrow Function in ES6

Arrow Function in ES6  An Arrow function is a new concept recommended in ECMAScript 2015. It is also called “Fat Arrow Function.” It helps us to write the function more accurately. This function...

5 minutes read.

Map Object in ES 6

Map Object in ES 6 ES 6 introduced a new feature that is included in JavaScript. In the previous versions of ECMAScript, if we want to perform mapping on values and...

5 minutes read.

Decision-Making in ES6 | ECMAScript 6

Decision-Making in ECMAScript 6 The Decision-making structure or statements are used to determine one or more than one condition to be evaluated or tested. The Decision-making statement helps us to decide...

7 minutes read.

Loop Control Statement in ES6

Loop Control Statement in ECMAScript 6 The loop control statements can be defined as a set of conditions that control the execution of the loop. It executes the loop until the condition becomes false....

6 minutes read.

Environment Set up in ES6

Environment Set up in ES6 JavaScript is a portable language. It can execute on any browser or any host, and any operating system. Before the use of JavaScript, we need to...

5 minutes read.

Boolean in ES6

Boolean in ES6 In ES6, the Boolean object is used to show two values either it may be true or false. In JavaScript, we can use the Boolean object as a...

4 minutes read.

Void Keyword in ES6

Void Keyword in ES6 In ES6, the void keyword is referred to as a function return type, but it does not return any value. It classifies the expression and returns the...

2 minutes read.

Promises in ES6

Promises in ES6 A promise is used to represent some aspects that are finally fulfilled. It can also be resolved or discarded according to the operational output. In ES6, a promise...

8 minutes read.

Object in ES6

Object in ES6 The term object can be defined as an instance that contains the group of key-value pairs. We can modify it using the life cycle of an object in...

6 minutes read.

ES6 Tutorial

Introduction of ES6 The ECMAScript 6 is a scripting language specification institutionalized by ECMA International. ECMA, named in 1994, is an international organization, related to information and communication system. ECMA stands for “European Computer Manufacturers...

8 minutes read.