<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shane Plasebo</title>
    <description>The latest articles on DEV Community by Shane Plasebo (@plasebo).</description>
    <link>https://dev.to/plasebo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F247639%2F84d69c2d-a028-4a10-aeea-f1bf14b5ce6f.png</url>
      <title>DEV Community: Shane Plasebo</title>
      <link>https://dev.to/plasebo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/plasebo"/>
    <language>en</language>
    <item>
      <title>How to create an immediately-invoked function expression - IIFE in Javascript</title>
      <dc:creator>Shane Plasebo</dc:creator>
      <pubDate>Fri, 08 Oct 2021 07:18:44 +0000</pubDate>
      <link>https://dev.to/plasebo/how-to-create-an-immediately-invoked-function-expression-iife-in-javascript-4f1l</link>
      <guid>https://dev.to/plasebo/how-to-create-an-immediately-invoked-function-expression-iife-in-javascript-4f1l</guid>
      <description>&lt;p&gt;An Immediately-invoked Function Expression is a way to execute a function immediately, as soon as it is created. IIFE is an example of function invocation: the first pair of parenthesis (function(name) {...}) is an expression that evaluates to a function object, followed by the pair of parenthesis with parameters if any.&lt;/p&gt;

&lt;p&gt;Let say we have a function to calculate a total from an array:&lt;/p&gt;

&lt;p&gt;The simplest way is to create a function with a for..in loop to loop into each values of the array. Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function calculateTotal(arr) {
   var total = 0;
   for (var i in arr) {
     total += arr[i];
   }
   return total;
}

calculateTotal([1,2,3,4,5,6]); // invoke the Javascript function for execution
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How to construct the immediately-invoked function expression - IIFE?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First wrap the whole function declaration into parenthesis&lt;/li&gt;
&lt;li&gt;Secondly add 1 more parenthesis at the end of the function.&lt;/li&gt;
&lt;li&gt;If there is any parameters to pass, pass them in the last parenthesis from step 2&lt;/li&gt;
&lt;li&gt;If there is any Javascript invocation like calculateTotal([1,2,3,4,5]);, please remove it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(function calculateTotal(arr) {
var total = 0;
for (var i in arr) {
  total += arr[i];
}
return total;
}) // wrap the whole function into parenthesis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(function calculateTotal(arr) {
var total = 0;
for (var i in arr) {
  total += arr[i];
}
return total;
})() // add 1 more parenthesis here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(function calculateTotal(arr) {
var total = 0;
for (var i in arr) {
  total += arr[i];
}
return total;
})([1,2,3,4,5,6]) // pass parameters here
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
