Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 29, 2021 08:04 am GMT

Enough JavaScript to get you Started : 19 IIFE

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})();
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

Example : the new way

(function(){    alert('hello user ! how are you?');})();
Enter fullscreen mode Exit fullscreen mode

Example : Arrow functions as IIFE

( () => {      alert('hello user ! how are you?');})();
Enter fullscreen mode Exit fullscreen mode

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

Hey , Let' Connect

Twitter /
Github


Original Link: https://dev.to/whoadarshpandya/enough-javascript-to-get-you-started-19-iife-3j2k

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