IIFE

Immediately Invoked Function Expression

  • It is a function expression, invoked on its declaration.

  • We can place all the library codes to avoid namespace collision.

  • mostly used in the JavaScript libraries.

  • When it is invoked all the variables and functions will be available in our local environment.

  • The variables and functions cannot be accessed outside the function.

  • It is an anonymous function express executed in flat or assign to any variable.

  • It also increased the performance and saves some memory.

Ex 1:
(function() {
    var name = "John Deo";
})();

Ex 2:
const person = (function() {
    function fullname() {
        return "John Deo";
    }
    return {
        fullname: fullname
    };
})();
person // { fullname: fullname }
person.fullname() // John Deo

Last updated