IIFE(Immediately Invoked Function Expression)
IIFE = Immediately Invoked Function Expression
invokes itself when defined
normal JS function
function () {
// Code to run
};
Parentheses(fn) around the function tell JavaScript to treat the function as an expression.
(function () {
// Code to run immediately
})
Function expressions will execute automatically if the expression is followed by ().
(function () {
// Code to run immediately
})();
eg:
with arrow function () =>
(() => {
// Code runs immediately here
})();
src:
https://www.w3schools.com/js/js_function_iife.asp
JavaScript modules often replace the need for IIFEs.
Modules have their own scope by default
IIFEs cannot be called again.