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 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.

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 in ES6

Array in ES6 The array can be defined as an object that is used to display the set of homogenous type of elements in ECMAScript 6. The array provides the facility to store...

5 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.

Module in ES6

Module in ES6 In ES6, the modules can be defined as a small chunk of program code stored in a file. The modules are used to modularize the source code with...

6 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.

Class in ES6

Class in ES6: The Class is a basic term in object-oriented programming (OOPs). A class helps to determine the prototype for modeling in real-world objects. It is used to formulate...

4 minutes read.

Cookies in ES6

Cookies in ES6 The cookies are an old client-side storage technique produced in a server-side scripting language such as Php and ASP etc. The cookie can be defined as a small...

6 minutes read.

Functions in ES6 | ECMAScript 6

Functions in ECMAScript 6 In ECMAScript programming language, the term ‘function’ can be defined as a collection or group of input statements, used to perform particular computational calculations and generate the output...

9 minutes read.

Operators in ES6 | ECMAScript 6

Operators in ECMAScript 6 The term Operator is refers to a symbol that is used to instruct the computer system to perform a specific operation. The ECMAScript 2015 supports a rich set of operators....

11 minutes read.

Page Redirect in ES6

Page Redirect in ES6 The term redirect can be defined as a process to send the user and search engine on a specified URL from the original page. “The page redirection is...

3 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.

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.

Validation in ES6

Validation in ES6 The term validation can be defined as a mechanism to check the information given by the user is correct or not according to the requirements. If the data...

9 minutes read.

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.

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.

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.

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.

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.