Karan Chhabra6
Mega Sage

Hi @RudhraKAM ,

 

These type of functions are called 𝗜𝗺𝗺𝗲𝗱𝗶𝗮𝘁𝗲𝗹𝘆 𝗜𝗻𝘃𝗼𝗸𝗲𝗱 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻 (𝗜𝗜𝗙𝗘) or self invoking functions

A function that is 𝗶𝗻𝘃𝗼𝗸𝗲𝗱 𝗶𝗺𝗺𝗲𝗱𝗶𝗮𝘁𝗲𝗹𝘆 𝗮𝗳𝘁𝗲𝗿 𝗶𝘁 𝗶𝘀 𝗰𝗿𝗲𝗮𝘁𝗲𝗱 is called an IIFE, it is a stand-alone function with or without any name and no explicit invocation is required.

For invoking it, we simply 𝗮𝗱𝗱 (); 𝗮𝗳𝘁𝗲𝗿 𝘁𝗵𝗲 𝗲𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻, we can even pass arguments to it.

Every IIFE gets its 𝘀𝗲𝗽𝗮𝗿𝗮𝘁𝗲 𝗲𝘅𝗲𝗰𝘂𝘁𝗶𝗼𝗻 𝗰𝗼𝗻𝘁𝗲𝘅𝘁 - any variables or functions inside IIFE go into this execution context.

These variables are not in the global environment and even if any other variable or function exists with the same name - 𝘁𝗵𝗲𝘆 𝗱𝗼𝗻'𝘁 𝗶𝗻𝘁𝗲𝗿𝗳𝗲𝗿𝗲 𝘄𝗶𝘁𝗵 𝗲𝗮𝗰𝗵 𝗼𝘁𝗵𝗲𝗿. Variables and functions declared inside IIFE are 𝗽𝗿𝗶𝘃𝗮𝘁𝗲 𝗮𝗻𝗱 𝗶𝗻𝗮𝗰𝗰𝗲𝘀𝘀𝗶𝗯𝗹𝗲 𝘁𝗼 𝘁𝗵𝗲 𝗴𝗹𝗼𝗯𝗮𝗹 𝗲𝗻𝘃𝗶𝗿𝗼𝗻𝗺𝗲𝗻𝘁.

 

Here, executeRule is a named IIFE with arguments current and previous.

 

Refer to these docs as well: https://developer.mozilla.org/en-US/docs/Glossary/IIFE

https://www.geeksforgeeks.org/immediately-invoked-function-expressions-iife-in-javascript/

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks!