Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 22, 2020 02:39 pm GMT

IIFEs JS

Hello there guys!

Today we will speak about IIFEs and why sometimes could be handy for your projects.

IIFEs in javascript stands for immediately invoked function expression,in simple terms just a function that runs directly after of its statement.

Commonly we declare a function on the stack flow and when we need it we call it lets see a quick example.

Let's imagine that we have a function that we would like to show to us a console message,simple enough?

It could be something like

function showMessage() {console('Hello DevCommunity)};
Enter fullscreen mode Exit fullscreen mode

Right?
Right,now if we need to use this function we just say

showMessage();
Enter fullscreen mode Exit fullscreen mode

with ease we solved our problem.
But what happening if we want to initiate that function instantly.

In this condition it is where IIFEs be useful, lets see this kind of syntax for the exact example above.

(function () {console.log('Hello')}) ();
Enter fullscreen mode Exit fullscreen mode

As you can see,we wrap our function declaration inside parentheses and after the addition of functionality (console.log) , we close them and the curly braces as well, we add one more pair of parentheses out of the function scope that does our job, to invoke this function immediately.

Of course we can set parameters,so we can say

(function (text) {console.log('Hello'  + text)}) ('Community');
Enter fullscreen mode Exit fullscreen mode

I hope you got an idea for a better usage of IIFEs.

Have a nice workday guys, in case for further explanation do not hesitate to contact me or find me in github or linkedin.
GitHub : https://github.com/feco2019
Linkedin : https://www.linkedin.com/in/dimitris-chitas-930285191/


Original Link: https://dev.to/feco2019/iifie-js-16om

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To