<?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: Dharmarajsinh Jethva </title>
    <description>The latest articles on Forem by Dharmarajsinh Jethva  (@gokukun).</description>
    <link>https://forem.com/gokukun</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%2F499366%2F0aafa4b0-b494-4b43-affa-5eb6d754e6d3.jpeg</url>
      <title>Forem: Dharmarajsinh Jethva </title>
      <link>https://forem.com/gokukun</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gokukun"/>
    <language>en</language>
    <item>
      <title>Execution Model of JavaScript</title>
      <dc:creator>Dharmarajsinh Jethva </dc:creator>
      <pubDate>Sun, 26 Sep 2021 09:53:18 +0000</pubDate>
      <link>https://forem.com/gokukun/execution-model-of-javascript-3hko</link>
      <guid>https://forem.com/gokukun/execution-model-of-javascript-3hko</guid>
      <description>&lt;p&gt;JS has been often classified as a scripting interpreted language. However, the truth to this matter is that there are more than one ways of interpreting (pun intended &amp;amp; left to your interpretation).&lt;/p&gt;

&lt;h2&gt;
  
  
  Models of Execution
&lt;/h2&gt;

&lt;p&gt;The usual method that we think of, when the phrase &lt;em&gt;interpreted language&lt;/em&gt; is mentioned, is the line by line execution of the source code. In this processing model, each line is transformed into machine code, the transformed line of code is executed and only after that does the processing model continue to the next line.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz6nt4eemvogzlko2pisj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz6nt4eemvogzlko2pisj.png" alt="interpreted execution model"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is another processing model called &lt;em&gt;compilation&lt;/em&gt; where the entire source code is taken and transformed at once into machine instructions and these instructions are saved into another file. Once the compiled machine instructions file is created, executing this file will run the output of original code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl7ijg2bixh27zkc4z3d9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl7ijg2bixh27zkc4z3d9.png" alt="compiled execution model"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Is JS interpreted? A prologue to JS execution
&lt;/h2&gt;

&lt;p&gt;The question still remains that whether JS employs this method of line by line conversion of code, followed by execution, we commonly refer to as 'interpretation'? Well, the answer is little more subtle than a yes or a no answer. JavaScript engines have amalgamated both of these above mentioned processing models into how they execute JS. Even though, these engines don't generate a compiled machine instruction file, JS is still compiled before it starts executing. I know. I know. That was a lot to take in just one sentence but just give this idea &lt;a href="https://signalvnoise.com/posts/3124-give-it-five-minutes" rel="noopener noreferrer"&gt;five minutes&lt;/a&gt; and the pieces to the puzzle of JS's execution mechanism will suddenly start to fit. Keeping this idea in mind that JS first compiles the entire code, let's continue ahead.&lt;/p&gt;

&lt;p&gt;The behavior that JS compiles it's code first, is noticeable through something as plain as 'syntax errors' and 'hoisting'.&lt;/p&gt;

&lt;h3&gt;
  
  
  Making a Syntactical Error
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// this won't be printed&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;wrongJS&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;this will throw an error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If JS was interpreted, transformed, and executed line by line without moving to the next line before completing this process, the first line would've printed "Hello World" to the console because the error lies on line 2. But, it doesn't get executed line by line without getting compiled first and it didn't print to the console because of the syntax error. This is one such example portraying that there are certain elements of compilation at play here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hoisting a Function Declaration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;print_hello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;print_hello&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, if JS was interpreted line by line, it couldn't have looked ahead on line 3 without executing line 1. That would mean that JS didn't know what &lt;code&gt;print_hello()&lt;/code&gt; is on line 1 and it should've rightfully thrown a reference error. But, it didn't throw an error and instead, it successfully executed the function and printed to the console. &lt;/p&gt;

&lt;p&gt;These examples clearly poke some holes in the theory that JS is a strictly interpreted language. So, does that mean JS is entirely a compiled language? Hold your horses. As I said, JS engines implement a mixture of both these methods. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;From the evidence of the above give peculiar cases, it should suffice to say that JS engines have a compiler which compiles the code into a byte code and this byte code is then fed into an interpreter which generates a machine code to be executed. This is a high level explanation of how JS code gets run without getting into the details of the baseline compilers, JIT compilers, interpreters and what not.&lt;/p&gt;

&lt;p&gt;Fun fact: As JS engines don't have typical compilation step of being compiled ahead of time, the compiled code isn't always optimized because they don't always have as much time to optimize it. Hence, they use optimizing compilers to optimize the repeated pieces of code during the execution by keeping a track of executed code and the data that's used for execution.&lt;/p&gt;

&lt;p&gt;Hopefully, the idea about how JS engines execute code has started to make more sense. We'll explore this concept more in future post of scoping mechanisms.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>intepreted</category>
      <category>compiled</category>
    </item>
    <item>
      <title>Functions in JavaScript</title>
      <dc:creator>Dharmarajsinh Jethva </dc:creator>
      <pubDate>Sat, 25 Sep 2021 06:52:28 +0000</pubDate>
      <link>https://forem.com/gokukun/functions-in-javascript-3mp8</link>
      <guid>https://forem.com/gokukun/functions-in-javascript-3mp8</guid>
      <description>&lt;h2&gt;
  
  
  Functions - A Brief History
&lt;/h2&gt;

&lt;p&gt;In the advent of programming, we created our systems using routines and subroutines. This, later, transformed to the creation of systems using programs, subprograms and functions. And, these days, we're only left with functions, at least that's what the majority of the programming community knows and works with everyday.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Functions? Why are they needed?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Functions&lt;/em&gt; are a group of statements, packaged as a single entity, that performs a certain task. They can be called upon or "invoked" many times. They can receive zero or more inputs and return zero or more outputs.&lt;/p&gt;

&lt;p&gt;For example, let's say we want to square a few numbers.&lt;/p&gt;

&lt;p&gt;An implementation without the use of functions would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;number2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;square_of_number2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;number2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;number2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;square_of_number2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;number3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;square_of_number3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;number3&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;number3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;square_of_number3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's look at an implementation that is created using functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;square_of_number2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;square_of_number3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code looks so much cleaner just by comparing the above two methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages of Creating Functions
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Code looks clean and organized.&lt;/li&gt;
&lt;li&gt;DRY principle is followed.&lt;/li&gt;
&lt;li&gt;Enable reuse of code in the program.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Ways of creating Functions in JS
&lt;/h2&gt;

&lt;p&gt;There are 4 ways of creating functions in JavaScript, each with their own unique subtleties. The order below denotes my preference of using these methods, from most used to never used.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Function Declarations (always named)&lt;/li&gt;
&lt;li&gt;Function Expressions (can be named or anonymous)&lt;/li&gt;
&lt;li&gt;Arrow Functions (always anonymous)&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;Function&lt;/code&gt; constructor (don't ever do it; security reasons and performance issues)&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;To &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#defining_functions"&gt; quote MDN &lt;/a&gt;,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A function definition (also called a function declaration, or function statement) consists of the function keyword, followed by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The name of the function.&lt;/li&gt;
&lt;li&gt;A list of parameters to the function, enclosed in parentheses and separated by commas.&lt;/li&gt;
&lt;li&gt;The JavaScript statements that define the function, enclosed in curly brackets, {...}.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, if any JS statement starts literally with the &lt;em&gt;&lt;code&gt;function&lt;/code&gt;&lt;/em&gt; keyword, then it's a Function Declaration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why prefer using Function Declaration?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Standard syntax (found across languages)&lt;/li&gt;
&lt;li&gt;Only way to define a function which hoists&lt;/li&gt;
&lt;li&gt;Has a name attached to it; more readable error stack traces
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;calculateSquare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// hoisting&lt;/span&gt;
&lt;span class="c1"&gt;// OUTPUT: 4&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;calculateSquare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Parameters &amp;amp; Arguments
&lt;/h2&gt;

&lt;p&gt;The placeholder which accepts a value as an input in the function is called a Parameter.&lt;br&gt;
When declaring a function, we specify it's parameters.&lt;/p&gt;

&lt;p&gt;In the previous example code, &lt;code&gt;number&lt;/code&gt; is a parameter.&lt;/p&gt;

&lt;p&gt;The value passed into the function while it's being invoked is called an argument.&lt;/p&gt;

&lt;p&gt;In the previous example code, the value &lt;code&gt;2&lt;/code&gt; is an argument.&lt;/p&gt;
&lt;h3&gt;
  
  
  Default Parameters
&lt;/h3&gt;

&lt;p&gt;Parameters can be assigned a default value. This default value is used when an argument value isn't passed into the function or if &lt;code&gt;undefined&lt;/code&gt; is passed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;defaultParamFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;some value&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;defaultParamFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// no argument is passed&lt;/span&gt;
&lt;span class="c1"&gt;// OUTPUT: 'some value'&lt;/span&gt;

&lt;span class="nx"&gt;defaultParam&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;overridden&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// argument is passed&lt;/span&gt;
&lt;span class="c1"&gt;// OUTPUT: 'overridden'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Function Expressions
&lt;/h2&gt;

&lt;p&gt;A different approach to creating a function is by using the Function Expressions syntax.&lt;/p&gt;

&lt;p&gt;These can be of 2 types: &lt;strong&gt;Named&lt;/strong&gt; Function Expressions and &lt;strong&gt;Anonymous&lt;/strong&gt; Function Expressions.&lt;/p&gt;

&lt;p&gt;Named Function Expressions are very uncommon. Very rarely encountered but should be used the most.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// named function expression&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;variable_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;function_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// anonymous function expression&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;another_variable_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My advice is to always name any and all functions expressions. The same reasoning that is used for why one should always use function declaration follows here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why use named function expressions?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;More self documenting code&lt;/li&gt;
&lt;li&gt;More debuggable stack traces&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  IIFEs (Immediately Invoked Function Expressions)
&lt;/h2&gt;

&lt;p&gt;As the name suggests, these are function expressions which are invoked/called immediately where they're defined.&lt;/p&gt;

&lt;p&gt;The syntax involves wrapping the function into a set of parentheses () and then invoking it with another set of parentheses ().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// function definition is wrapped in parentheses&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;immediately_invoked&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;)();&lt;/span&gt; &lt;span class="c1"&gt;// notice the invoking set of parentheses&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Arguments can also be passed to IIFEs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;iife&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;param1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// argument 1 passed here&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;With arrow functions, &lt;code&gt;function&lt;/code&gt; keyword isn't used to define a function. Instead, a fat arrow &lt;code&gt;=&amp;gt;&lt;/code&gt; is used to define a function.&lt;/p&gt;

&lt;p&gt;Arrow functions are anonymous by definition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// syntax for defining an arrow function&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;function_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// more characters&lt;/span&gt;
    &lt;span class="c1"&gt;// function body&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// syntax for a normal function declaration&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;function_name&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// less characters&lt;/span&gt;
    &lt;span class="c1"&gt;// function body&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't use arrow functions as a generic replacement of function declaration just because they &lt;em&gt;seem&lt;/em&gt; shorter to write. Because if we just compare the two defined functions in the above example, it's clearly visible that arrow functions take more characters to write them as compared to writing the same function in Function Declaration way (compare the characters in the first line of declaration).&lt;/p&gt;

&lt;h3&gt;
  
  
  Don't use Arrow Functions. Why?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;They are syntactically anonymous which means while debugging, stack traces show errors in these functions as "Anonymous Function"&lt;/li&gt;
&lt;li&gt;It doesn't have a &lt;code&gt;this&lt;/code&gt; keyword. It binds &lt;code&gt;this&lt;/code&gt; lexically.(More about this in future posts)&lt;/li&gt;
&lt;li&gt;They only &lt;em&gt;seem&lt;/em&gt; shorter at a glance but take almost as much as or more characters than a function declaration.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Arrow Function IIFEs
&lt;/h2&gt;

&lt;p&gt;IIFEs can also be defined using the arrow function syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// notice the first wrapping parenthesis&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello World&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Function Constructor
&lt;/h2&gt;

&lt;p&gt;Functions can also be created using the Function Constructor syntax. But, it's advised not to use this syntax at all unless absolutely necessary. This creates function dynamically in the scope and these functions are only available to be called in the global scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// syntax

const variable_name = new Function('arg1', 'arg2', ..., 'argN', 'functionBody');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;square_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;return number*number;&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;square_number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// OUTPUT: 4&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fun fact: Function constructor can be invoked with or without the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Declaration vs Expression vs Arrow
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// 01 Function Declaration
function function_name() {
    // body
}

// 02-a Function Expression (Named)

var variable_name = function function_name() {
        // body
}


// 02-b Function Expression (Anonymous)

var variable_name = function() {
        // body
}


// 03 Anonymous Arrow Function

var variable_name = () =&amp;gt; {
    // body
}

// 04 Function Constructor

var variable_name = new Function('param_name', 'function body');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To summarize, there are 4 different ways of creating functions in JavaScript. &lt;/p&gt;

&lt;p&gt;But, (in my opinion) prefer using Function Declaration over other types of function designs. Named Function Expression can also be used. Don't use Anonymous Function Expression and Arrow Function unless absolutely necessary and definitely not as a generic replacement for Function Declaration. Never use Function Constructor to create a function.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>functions</category>
      <category>lexicalscope</category>
    </item>
  </channel>
</rss>
