Open In App

What are IIFE (Immediately Invoked Function Expressions) ?

Immediately Invoked Function Expressions (IIFE) are JavaScript functions that are defined and immediately invoked. They are wrapped in parentheses to turn them into expressions and followed by an additional pair of parentheses to invoke them immediately after declaration.

Syntax:

(function() {
// IIFE body
})();

Key characteristics of IIFE:



Example: Here, the function is immediately invoked after its declaration, and the code inside it runs immediately. This pattern helps in keeping variables within a local scope and avoids conflicts with variables in the global scope.




(function() {
    let message = 'Hello, World!';
    console.log(message);
})();

Output

Hello, World!
Article Tags :