<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Moe Sadoon on Medium]]></title>
        <description><![CDATA[Stories by Moe Sadoon on Medium]]></description>
        <link>https://medium.com/@moesadoon?source=rss-1f369e132d6c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*j5dG975CBleSyiR3.jpg</url>
            <title>Stories by Moe Sadoon on Medium</title>
            <link>https://medium.com/@moesadoon?source=rss-1f369e132d6c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 15:05:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@moesadoon/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Immediately Invoked Function Expressions — all a bit iffy?]]></title>
            <link>https://medium.com/@moesadoon/immediately-invoked-function-expressions-all-a-bit-iffy-2bfcf775572?source=rss-1f369e132d6c------2</link>
            <guid isPermaLink="false">https://medium.com/p/2bfcf775572</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[function]]></category>
            <category><![CDATA[es5]]></category>
            <dc:creator><![CDATA[Moe Sadoon]]></dc:creator>
            <pubDate>Mon, 26 Jun 2017 21:31:35 GMT</pubDate>
            <atom:updated>2017-06-28T21:15:00.282Z</atom:updated>
            <content:encoded><![CDATA[<p>My first encounter with immediately invoked function expressions (or IIFEs for short) left me feeling somewhat bemused.</p><p>For those who don’t know what I’m talking about, they look a little like this:</p><pre>(function(name){<br>   console.log(&quot;hello &quot; + name);<br>})(&quot;Moe&quot;);</pre><p>Besides making a neat drinking game for every time you spot a parenthesis, there’s a profound logic to this. But before addressing them, its worth quickly reviewing the different types of function objects JavaScript allows us to create.</p><h3><strong>Function statements and function expressions</strong></h3><p>The first function object format you’ll probably come across are <strong>function statements/declarations</strong> such as the example below:</p><pre>function greet(name) {<br>   console.log(&quot;hello &quot; + name);<br>}</pre><pre>greet(&quot;Moe&quot;); //=&gt; this prints &quot;<strong>hello Moe</strong>&quot;</pre><p>These function objects are <strong>hoisted</strong> during the creation phase of the execution context they’re in, meaning that they’re available to use at any point of the code. So you can even invoke the function before it is declared:</p><pre>greet(&quot;moe&quot;); // =&gt; This will also print &quot;<strong>hello Moe</strong>&quot;</pre><pre>function greet(name) {<br>   console.log(&quot;hello &quot; + name)<br>}</pre><p>The other way of creating function objects is by using <strong>function expressions </strong>seen below:</p><pre>var greet = function(name) {<br>   console.log(&quot;hello &quot; + name)<br>}</pre><p>The key difference here being that instead of declaring a function beforehand, we are assigning it to a variable. You’ll also notice the absence of a function name, instead we have an <strong>anonymous function</strong> assigned to the variable.</p><p>And unlike function declarations, function expressions are not hoisted on creation, and the variable assigned to it is set to <strong>undefined</strong> and is only assigned to the newly created function object when the parser reaches that line of code.</p><p>This also means we cannot invoke the function before it is defined like with function declarations, and hence it must always be invoked <strong><em>after</em></strong> being defined:</p><pre>greet(&quot;moe&quot;); // =&gt; this will return an error</pre><pre>var greet = function(name) {<br>   console.log(&quot;hello &quot; + name)<br>}</pre><pre>greet(&quot;moe&quot;); // =&gt; this will print &quot;<strong>hello moe</strong>&quot;</pre><p>It is a function <strong>expression</strong> because in JavaScript, an expression is something that returns a value. In this case the value being returned is the function object itself, which is created ‘on the fly’ upon the parser reaching that line of code.</p><h3>So what is an immediately invoked function expression?</h3><p>Quite simply, an IIFE is a function expression that is <strong>immediately invoked </strong>after creation. Let’s take a look at the earlier example again:</p><pre>(function(name) {<br>   console.log(&quot;hello &quot; + name<br>})(&quot;moe&quot;);                          </pre><pre>//=&gt; this prints &quot;hello moe&quot;    </pre><p>You’ll notice the parentheses ‘()’ right at the end used to invoke the function with an argument. This means that this function is immediately executed after it is defined and hence it will print “hello moe” to the console without the need to invoke it separately, e.g: greet(“moe”);</p><p>So why is it a <strong>function expression?</strong></p><p>Well, remember how I said expressions in JavaScript return a value? Well that’s exactly what we’re doing here — returning a function object on the fly, and this is made possible because of the parentheses wrapping around the whole function.</p><p>See the syntax parser can be a bit too clever for its own good, and in the absence of the parentheses, the first characters it would come across is the ‘function’ keyword, meaning that the parser will then expect us to be creating a function declaration. However by wrapping the function inside parentheses, we are tricking the parser into thinking this is an expression, hence returning a value which in this case will be the function object.</p><h3><strong>Why bother?</strong></h3><p>So now that we know what they are, like me you may have been wondering — Okay cool, but so what?</p><p>Well it all comes down to <strong>privacy </strong>and preventing variable <strong>conflict. </strong>This is because the variables declared inside an IIFE are only accessible inside of its scope and hence cannot be accessed in the outside environment:</p><pre>(function(){<br>   var nuclearCodes = 1234;<br>})();</pre><pre>console.log(nuclearCodes) //=&gt; this returns an error</pre><p>This also works both ways, in that your variables within the IIFE are not at risk of conflicting with variables on the outer environment which may share a similar name. Hence why you would find many frameworks/libraries such as jQuery wrapped within an IIFE so it can be seamlessly injected into an application without interefering with the application code.</p><p>Though you may be thinking why not just use a conventional function instead and invoke it? Well by opting for an IIFE, you prevent the need to pollute the global name space with unnecessary variable names and also mitigate the risk of accidentally calling a function elsewhere.</p><h3><strong>That’s it, for now.</strong></h3><p>Well there you have it. IIFEs are yet another pretty sweet tool in the JavaScript toolset. There’s the suggestion that they may find themselves being less prevalent in ES6, but that’s something I’ll have to explore myself.</p><p>Until then.</p><p>M.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2bfcf775572" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>