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

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.

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.

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.

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.

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.

Variables in ES6 | ECMAScript 6

Variables in ECMAScript 6 A Variable is a place in memory used to store a value. There are some rules for declaration of the variable- Variable Initialization Initialization is a process of locating and storing the initial...

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

String in ES6

String in ES6 The term string can be defined as an object used to represent the sequence of the characters. The string is widely used to store a text-based value like...

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

Set in ES6

Set in ES6 A Set is a kind of object used to create a group of unique values. The values stored in a set can be string, literal, array, and integer....

6 minutes read.