Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Demystifying JavaScript IIFE

Posted on Oct 3 • Originally published at arindam1729.hashnode.dev Immediately Invoked Function Expression (IIFE) is one of the most popular design patterns in JavaScript. It pronounces like "iify".The Name sounds Complex?Don't worry! In this article we'll understand what is IIFE, It's usecase and many more! After completing this article you'll have a clear understanding of IIFE.IIFE stands for Immediately Invoked Function Expression ."An IIFE (Immediately Invoked Function Expression) is a JavaScript function that is executed immediately after it is defined. It is a self-invoking function that is commonly used to create a private scope, encapsulate code, and prevent variable pollution in the global namespace."Let's understand the syntax:Have you noticed the Difference here??Here we have wrapped the function with parenthesis and then invoked it.But why?We wrap the function with parentheses to treat the function as an expression rather than a function declaration. And we use another set of parentheses to invoke the function immediately.But What will happen if we don't use the outer parenthesis?Let's see this with an example,This throws an error simply because in the normal function syntax we can't invoke a function directly. For this case, we have to invoke the function explicitly.So to use IIFE, we need to use the outer parenthesis.There are many reasons to use IIFE. Here are some of themIIFEs create a new function scope, which helps to prevent variable and function name collisions with other parts of the code. Variables defined inside an IIFE are not accessible from the outside scope, keeping them private and isolated.As you know, variables declared in the global scopes are accessible by every function. Sometimes this creates conflicts and errors. To avoid this, IIFE is very useful, as It creates its own scope and keeps them safe from Global scope pollutionIIFEs are useful for executing code that needs to run only once during the application's initialization.If you found this blog post helpful, please consider sharing it with others who might benefit. You can also follow me for more content on Javascript and other web development topics.To sponsor my work, please visit: Arindam's Sponsor Page and explore the various sponsorship options.Connect with me on Twitter, LinkedIn, Youtube and GitHub.Thank you for Reading :)Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Digital Samba - Oct 3 Hardik Radadiya - Oct 3 Akshay Rao - Oct 3 Aravind Manikandan - Oct 3 Once suspended, arindam_1729 will not be able to comment or publish posts until their suspension is removed. Once unsuspended, arindam_1729 will be able to comment and publish posts again. Once unpublished, all posts by arindam_1729 will become hidden and only accessible to themselves. If arindam_1729 is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Arindam Majumder . They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag arindam_1729: arindam_1729 consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging arindam_1729 will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Demystifying JavaScript IIFE

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×