IIFE โจ
๐ IIFE or immediately invoked functions as expressions
simply refers to a function which runs as soon as it is defined.
๐ Which means if you have to write a function which runs in beginning of your web app , you can use IIFE.
๐ in early days if we want to do something like this we need to define a function and call it...
๐ but with IIFE design pattern the syntax and the code makes much more sense.
๐ IIFE takes 2 parentheses , one is meant for defining a anonymous function and another is meant to call the anonymous function.
๐ Syntax
(
// anonymous function
function () {
//function body
})();
๐ We'll create one IIFE which will greet user as soon as he/she comes to our website ๐
๐
Example : the old way
function greet () {
alert('hello user ! how are you?');
}
greet();
๐ Example : the new way
(function(){
alert('hello user ! how are you?');
})();
๐ Example : Arrow functions as IIFE
( () => {
alert('hello user ! how are you?');
})();
Let me know in comment section if you have any doubt or feedback. it's always worth to give time to the thriving developer community :)
Keep Coding โค
Top comments (1)
Nicely explained.