What is Immediately Invoked Function Expression - IIFE
IIFE is a function expression that automatically invokes after completion of the definition
const hello = 'Hello';
const to = 'World';
(function () {
const to = 'Peter';
console.log(`${hello} ${to}`);
})();
Output:
Hello Peter
Advantages of IIFE:
- 1. Do not create unnecessary global variables and functions
- 2. Functions and variables defined in IIFE do not conflict with other functions & variables even if they have same name.
- 3. Organize JavaScript code.
- 4. Make JavaScript code maintainable