Javascript Function IIFE

Introduction

IIFE stands for Immediately-invoked Function Expression.

The following code defines two functions:

// function statement
function greet(name) {
    console.log("Hello " + name);
}
greet("John");//ww w  . j  a v  a  2s  .  com

// function expression
var x = function (name) {
    console.log("Hello " + name);
}
x("John");

Here, the function is a fn expression but its not invoked

(function (name) {
    console.log("Hello " + name);
});

Here, the function is a fn expression and it is invoked without param

(function (name) {
    console.log("Hello " + name);
}());

Output:Hello undefined

Here, the function is a fn expression and it is invoked with param

(function (name) {
    console.log("Hello " + name);
}("John"));

Output:Hello John




PreviousNext

Related