<?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: Pragma coder</title>
    <description>The latest articles on DEV Community by Pragma coder (@pragmacoder).</description>
    <link>https://dev.to/pragmacoder</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%2F711657%2Fefd652e9-50a4-4d18-be2e-0988dbcb944d.JPG</url>
      <title>DEV Community: Pragma coder</title>
      <link>https://dev.to/pragmacoder</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pragmacoder"/>
    <language>en</language>
    <item>
      <title>Functions in JS and it's use cases</title>
      <dc:creator>Pragma coder</dc:creator>
      <pubDate>Mon, 17 Jan 2022 13:01:11 +0000</pubDate>
      <link>https://dev.to/pragmacoder/functions-in-js-and-its-use-cases-25aj</link>
      <guid>https://dev.to/pragmacoder/functions-in-js-and-its-use-cases-25aj</guid>
      <description>&lt;h2&gt;
  
  
  Function types
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Function declaration&lt;/li&gt;
&lt;li&gt;Function expression&lt;/li&gt;
&lt;li&gt;Arrow Functions&lt;/li&gt;
&lt;li&gt;Immediately Invoked Function expression&lt;/li&gt;
&lt;/ol&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function foo(){
     console.log("Do not use var");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Function declarations are loaded before the script is loaded. This means that the function can be invoked before the function declaration.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foo();    //Function will be invoked

function foo(){
     console.log("Please use strict in JS");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because function declarations are hoisted(Hoisting in JS refers to the process in which functions, variables or classes are moved to the top of the scope for interpreting, before the execution of the script.)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Function declarations also have a block scope in &lt;a href="https://javascript.info/strict-mode"&gt;"use strict"&lt;/a&gt; mode.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use strict";

if(x&amp;gt;18){

   foo();     //Will be invoked

   function foo(){
       console.log("Pineapples on pizza should be illegal.")
   }

   foo();     //Will be invoked
}

foo();     //Line 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Line 10 will throw a ReferenceError.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Consider the same case, without "use strict".
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foo();     //Will not be invoked

if(x&amp;gt;18){

   foo();     //Will be invoked

   function foo(){
       console.log("Pineapples in pizza should be illegal.")
   }

   foo();     //Will be invoked
}

foo();     //Will be invoked only when if block is executed.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function &lt;code&gt;foo&lt;/code&gt; declaration will not be hoisted in the global scope, but within it's local scope(within the if block).&lt;/p&gt;

&lt;p&gt;Function declarations are preferred when we want the function to available throughout it's scope for invoking.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function expressions
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let foo=function(){
     console.log("Elon Musk");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
Function expressions basically means assigning a function to a variable.(The foo variable is referenced to an Anonymous function) Unlike Function declarations, Function expressions are not hoisted.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foo();     //Will not be invoked

let foo=function(){
     console.log("Elon Musk.");
}

foo();     //Will be invoked
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The function is declared when the interpreter reads the right of the assignment operator(=).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Function expressions are preferred over function declarations when we want functions to have a limited scope instead of a global scope throughout the program.(They cannot be hoisted to prevent functions to be crowded in the global scope).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Arrow Functions
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foo=()=&amp;gt;{
     console.log("Web3");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
The arrow functions are a shorthand to writing functions. Let us understand it with the help of &lt;a href="https://javascript.info/settimeout-setinterval"&gt;&lt;code&gt;setTimeout()&lt;/code&gt;&lt;/a&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setTimeout(foo(),2000);     //Calls foo() after 2 seconds

function foo(){
     console.log("Mark is the watcher");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now check this out.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setTimeout(()=&amp;gt;{
     console.log("DC is underrated");
},2000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both the code snippets have the same functionality, but the latter has less lines of code and has better readability.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mulTwo = number =&amp;gt; number * 2  

/*If the arrow function has only one line in the body, there is no need for curly braces.*/

mulTwo(2);     //4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  "this" in Arrow Functions
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
Arrow functions do not have &lt;code&gt;this&lt;/code&gt;. If used, they take the context of the outer scope.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj={
     name:"Spiderman",

     foo(){
          setTimeout(()=&amp;gt;{
               console.log(this);
          },1000);
     }
};

obj.foo();                //Output is object obj.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;this&lt;/code&gt; refers to the &lt;code&gt;this&lt;/code&gt; of foo().&lt;/p&gt;

&lt;p&gt;For function expressions and function declarations the value of &lt;code&gt;this&lt;/code&gt; is dynamic(It's value depends upon where the function is being called).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj={
     name:"Spiderman",

     foo(){
          setTimeout(function(){
               console.log(this);  
          },1000);                              
     }
};

obj.foo();          //Window object

/*Output is the window object because the function is called in the global scope.*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order for the function to have the same context as it's scope, it needs to be bound to it's current context.(Using bind 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 obj={
     name:"Spiderman",

     foo(){
          setTimeout(function(){
               console.log(this);  
          }.bind(this),1000);                              
     }
};

obj.foo();
/*Output is the object obj because the function 
is now bound to the current context.*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the arrow functions do not have &lt;code&gt;this&lt;/code&gt;, it will use the value of &lt;code&gt;this&lt;/code&gt; from it's parent scope.&lt;/p&gt;

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

&lt;p&gt;An IIFE is called immediately after function is created. It is self invoking.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Consider this scenario,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let sum = (function(a,b){
    return a + b;
})(5, 10);

console.log(sum);       //15

console.log(sum());     //Error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above scenario, &lt;code&gt;sum&lt;/code&gt; only stores the return value of the function assigned to it, we cannot call the function elsewhere.&lt;/p&gt;

&lt;p&gt;An IIFE can also be named, even then it cannot be called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(function NWH() {
    console.log("I am a very good lawyer");
})();

NWH();      //NWH is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;IIFEs can be used if you want a function to be used only once. It is also used in closures(Click &lt;a href="https://mariusschulz.com/blog/use-cases-for-javascripts-iifes"&gt;here&lt;/a&gt; for a detailed article).&lt;/p&gt;

&lt;p&gt;Hope this article helped. I am new to programming and this is my first blog. Let me know how I can improve in the comments below!:D&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>functional</category>
    </item>
  </channel>
</rss>
