<?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>Forem: Ryan Smith</title>
    <description>The latest articles on Forem by Ryan Smith (@ryansmith).</description>
    <link>https://forem.com/ryansmith</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%2F108122%2F84dd73bd-8d8d-4af8-99f3-61ba17a06e14.jpg</url>
      <title>Forem: Ryan Smith</title>
      <link>https://forem.com/ryansmith</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ryansmith"/>
    <language>en</language>
    <item>
      <title>ELI5: Why use a function declaration, expression, or an IIFE in JavaScript?</title>
      <dc:creator>Ryan Smith</dc:creator>
      <pubDate>Sat, 23 Feb 2019 05:10:10 +0000</pubDate>
      <link>https://forem.com/ryansmith/eli5-why-use-a-function-declaration-expression-or-an-iife-in-javascript-378p</link>
      <guid>https://forem.com/ryansmith/eli5-why-use-a-function-declaration-expression-or-an-iife-in-javascript-378p</guid>
      <description>&lt;p&gt;I have seen functions in JavaScript declared in different ways. I have been trying to look up why, but answers that I have found only seem to state the difference between them but not the advantages of each.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My question for DEV users is &lt;em&gt;why&lt;/em&gt; use one style over the other?&lt;/strong&gt; What are the benefits over a classic function declaration? What are some code examples that show this?&lt;/p&gt;

&lt;p&gt;Consider the examples below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function Declaration
&lt;/h2&gt;

&lt;p&gt;To me, this is the "standard" way to define a function.&lt;/p&gt;

&lt;p&gt;My understanding is that a function declaration is hoisted, so it can be called before the function is defined. I expect a function to behave that way, so this is my preferred way to create a function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function calculateAverage() {
  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Function Expression
&lt;/h2&gt;

&lt;p&gt;A function expression assigns the function to a variable.&lt;/p&gt;

&lt;p&gt;My understanding of a function expression is that it will not be hoisted so it can only be executed after it is defined, but I'm unsure of why that would be useful.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const calculateAverage1 = function calculateAverage() {
  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have also seen this done with an arrow function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const calculateAverage1 = () =&amp;gt; {
  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Immediately Invoked Function Expression (IIFE)
&lt;/h2&gt;

&lt;p&gt;An IIFE will run as soon as it is defined.&lt;/p&gt;

&lt;p&gt;This one, I'm not too sure on. Why put that code in a function? It allows the creation of a scope, but I'm not sure when that would be useful.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(function () {
    ...
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>explainlikeimfive</category>
      <category>help</category>
      <category>requestforpost</category>
    </item>
  </channel>
</rss>
