This content originally appeared on DEV Community and was authored by Mayank Yadav
?IIFE: Immediately Invoked Function Expression
✔It is a JavaScript Function that runs as an when it is defined.
Let's talk about the two parenthesis in the above syntax.
( function() {} )
?Outer parenthesis '()'?
✅While executing JavaScript code, whenever the compiler sees the word function, it assumes that we are declaring a function in the code.
✅Therefore, if we do not use the first set of parentheses, the compiler throws an error because it thinks we are declaring a function, and by the syntax of declaring a function, a function should always have a name.
⚠So, instead of getting error, we have to use the first set of parenthesis that tells the compiler that this function is not the function declaration but it's function expression.
( function() {}) ();
?Right-end side parenthesis '()'?
✅So, IIFE states that the function should invoke immediately as soon as it is defined.
✅And as we know to run a function we need to invoke it.
✅If we don't invoke it, function declaration is returned.
✅That's why this second parenthesis is just for invoking.
This content originally appeared on DEV Community and was authored by Mayank Yadav
Mayank Yadav | Sciencx (2021-07-27T10:12:39+00:00) #8) What is IIFE in JavaScript??. Retrieved from https://www.scien.cx/2021/07/27/8-what-is-iife-in-javascript%f0%9f%90%b1%e2%80%8d%f0%9f%91%a4/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.