#8) What is IIFE in JavaScript?‍?

?IIFE: Immediately Invoked Function Expression

✔It is a JavaScript Function that runs as an when it is defined.

Syntax of IIFE?

Let’s talk about the two parenthesis in the above syntax.

( function() {} )

?Outer parenthesis ‘()’?
✅While …


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.

Syntax of IIFE?
image

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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » #8) What is IIFE in JavaScript?‍?." Mayank Yadav | Sciencx - Tuesday July 27, 2021, https://www.scien.cx/2021/07/27/8-what-is-iife-in-javascript%f0%9f%90%b1%e2%80%8d%f0%9f%91%a4/
HARVARD
Mayank Yadav | Sciencx Tuesday July 27, 2021 » #8) What is IIFE in JavaScript?‍?., viewed ,<https://www.scien.cx/2021/07/27/8-what-is-iife-in-javascript%f0%9f%90%b1%e2%80%8d%f0%9f%91%a4/>
VANCOUVER
Mayank Yadav | Sciencx - » #8) What is IIFE in JavaScript?‍?. [Internet]. [Accessed ]. Available 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/
CHICAGO
" » #8) What is IIFE in JavaScript?‍?." Mayank Yadav | Sciencx - Accessed . https://www.scien.cx/2021/07/27/8-what-is-iife-in-javascript%f0%9f%90%b1%e2%80%8d%f0%9f%91%a4/
IEEE
" » #8) What is IIFE in JavaScript?‍?." Mayank Yadav | Sciencx [Online]. Available: https://www.scien.cx/2021/07/27/8-what-is-iife-in-javascript%f0%9f%90%b1%e2%80%8d%f0%9f%91%a4/. [Accessed: ]
rf:citation
» #8) What is IIFE in JavaScript?‍? | Mayank Yadav | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.