<?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: Pawan Bhatt 👨‍💻</title>
    <description>The latest articles on Forem by Pawan Bhatt 👨‍💻 (@bhattpawan).</description>
    <link>https://forem.com/bhattpawan</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%2F668795%2F09b47737-b6ee-4e78-b951-cefc44469079.jpg</url>
      <title>Forem: Pawan Bhatt 👨‍💻</title>
      <link>https://forem.com/bhattpawan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/bhattpawan"/>
    <language>en</language>
    <item>
      <title>What is "this"?</title>
      <dc:creator>Pawan Bhatt 👨‍💻</dc:creator>
      <pubDate>Tue, 03 Aug 2021 13:30:52 +0000</pubDate>
      <link>https://forem.com/bhattpawan/what-is-this-4j66</link>
      <guid>https://forem.com/bhattpawan/what-is-this-4j66</guid>
      <description>&lt;p&gt;"this" is probably one of the most confusing topics for every JavaScript learner, mainly because of its weird behavior. But today, Iets try to get &lt;em&gt;a basic understanding&lt;/em&gt; of the &lt;em&gt;this&lt;/em&gt; keyword.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is &lt;em&gt;this&lt;/em&gt;?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;this&lt;/strong&gt;, is nothing but a special variable that is created for every &lt;a href="https://bhattpawan.hashnode.dev/javascript-behind-the-scenes-the-execution-context-and-call-stack"&gt;Execution Context&lt;/a&gt;. It generally points to the &lt;em&gt;owner of the function&lt;/em&gt;. Value is assigned to &lt;em&gt;this&lt;/em&gt; when the function is actually called. I know you're like Whaaaaaaat?&lt;br&gt;
&lt;a href="https://i.giphy.com/media/lkdH8FmImcGoylv3t3/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/lkdH8FmImcGoylv3t3/giphy.gif" alt="Whaaaaaat????"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not to worry, lets dive a bit deeper and you'll see everything starts making sense.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;em&gt;this&lt;/em&gt; in different scenarios:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;In the Global Scope: &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the Global Scope i.e. outside any function, the &lt;em&gt;this&lt;/em&gt; keyword points to the &lt;em&gt;window&lt;/em&gt; object.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WcDjKiBU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627989969453/KobpyU3Qn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WcDjKiBU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627989969453/KobpyU3Qn.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; In a method: &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In a method i.e. for a function defined inside an object, the &lt;em&gt;this&lt;/em&gt; keyword points to the object which is calling the method. For instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myObj = {
  getName: function () {
    console.log(this);
  },
};

console.log(myObj.getName());

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hdYOQynZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627991768239/cg46UNHNL.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hdYOQynZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627991768239/cg46UNHNL.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, we can use this now like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--o2TBUx_U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627991853304/lVCmNT7ER0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--o2TBUx_U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627991853304/lVCmNT7ER0.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, if we talk of arrow functions, we still get the window object for the simple reason that arrow functions do not have their own &lt;em&gt;this&lt;/em&gt;. It simply inherits the &lt;em&gt;this&lt;/em&gt; from its parent scope, which in this case, is 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;const myNewObj = {
  name: 'Pawan',
  getName: () =&amp;gt; {
    console.log(this);
  },
};
myNewObj.getName();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5ge0xlwd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627995373011/zLfyiw-eQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5ge0xlwd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627995373011/zLfyiw-eQ.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Simple function call: &lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;For sloppy / "non-strict mode":&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In a simple function / function expression which is defined in the Global Scope, the &lt;em&gt;this&lt;/em&gt; keyword again points to the &lt;em&gt;window&lt;/em&gt; object, since, by default, it is the object which is calling the 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 myFun = function(){
  console.log(this);
}
myFun();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DZl_ZcFd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627990157121/AIICGerwm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DZl_ZcFd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627990157121/AIICGerwm.png" alt="image.png"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SzWjzY36--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627990213862/52uteZRee.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SzWjzY36--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627990213862/52uteZRee.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;** For strict mode: **&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For strict mode though, the behaviour of &lt;em&gt;this&lt;/em&gt; is a bit different as in this case, it contains a value undefined. This is because in strict mode, JavaScript checks if the &lt;em&gt;this&lt;/em&gt; is bound to any object (as it does not automatically bind it to the global object), and hence we get undefined.&lt;br&gt;
&lt;/p&gt;

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

const myFun = function () {
  console.log(this);
};
myFun();

function myfun() {
  console.log(this);
}
myfun();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9bcmywb7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627990772786/_0c7PND4VO.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9bcmywb7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627990772786/_0c7PND4VO.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Arrow functions: 
For arrow functions, the &lt;em&gt;this keyword&lt;/em&gt; picks up the value of its parent scope / parent function. This is because the arrow functions do not have their own &lt;em&gt;this&lt;/em&gt;.  This inherited &lt;em&gt;this&lt;/em&gt; is known as &lt;strong&gt;lexical this&lt;/strong&gt; because it is inherited from the owner of the function, which in our case in the window object.
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rQ_xILIH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627991021482/Q6FrUFCq0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rQ_xILIH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627991021482/Q6FrUFCq0.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Understanding &lt;em&gt;this&lt;/em&gt; is a must if one is getting into JavaScript. However, only reading the articles won't help much, unless you get your hands dirty. Remember, practice is the key. So make up your own examples and analyze the value of &lt;em&gt;this&lt;/em&gt; for different scenarios. Feel free to use the comments section if you got something to add. &lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks a lot for reading &lt;em&gt;this&lt;/em&gt; article. Stay Safe &amp;amp; Happy Learning 🙌
&lt;/h2&gt;

&lt;p&gt;Liked what you read? do click that "Follow" button for more such writeups.&lt;/p&gt;

&lt;p&gt;Find me on &lt;a href="https://twitter.com/vaayouu"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript Behind The Scenes: Hoisting &amp; The Temporal Dead Zone</title>
      <dc:creator>Pawan Bhatt 👨‍💻</dc:creator>
      <pubDate>Sun, 01 Aug 2021 14:44:55 +0000</pubDate>
      <link>https://forem.com/bhattpawan/javascript-behind-the-scenes-hoisting-the-temporal-dead-zone-4j0e</link>
      <guid>https://forem.com/bhattpawan/javascript-behind-the-scenes-hoisting-the-temporal-dead-zone-4j0e</guid>
      <description>&lt;p&gt;After being comfortable with &lt;a href="https://dev.to/bhattpawan/javascript-behind-the-scenes-scope-the-scope-chain-4de7"&gt;Scope and The Scope Chain&lt;/a&gt;, lets spend some time understanding Hoisting, which is one of the most important yet misunderstood concepts of JavaScript. So without wasting any more time, lets jump right in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lets define Hoisting
&lt;/h2&gt;

&lt;p&gt;Hoisting is a phenomena in JavaScript because of which some variables can be accessed/used before they are actually declared. So is hoisting any type of magic or something? Its actually not. Hoisting seems no more like a magic once we are aware of &lt;a href="https://dev.to/bhattpawan/javascript-behind-the-scenes-the-execution-context-call-stack-4m64"&gt;JavaScript Execution Context&lt;/a&gt;. Let us see hoisting in practice. &lt;/p&gt;

&lt;p&gt;Consider the code snippet below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(`My name is ${name}`);
var name = 'Pawan';

// Output: My name is undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ideally, the code above should have thrown an error stating that the variable is not defined, but it doesn't and this is because of Hoisting. Let us see what actually happens.&lt;/p&gt;

&lt;p&gt;When the code is run, during the creation phase of Execution Context, the whole code is scanned before any line is executed and every variable is assigned a special value i.e., undefined and it is during the Execution Phase that the variables are actually assigned the values. &lt;a href="https://dev.to/bhattpawan/javascript-behind-the-scenes-the-execution-context-call-stack-4m64"&gt;[More Details on Execution Context Here]&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hoisting: Different Scenarios
&lt;/h2&gt;

&lt;p&gt;Though hoisting may seem very straightforward, it does have some things to be taken care of:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Function Declarations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hoisted -✔&lt;/li&gt;
&lt;li&gt;Initial Value - Actual Function&lt;/li&gt;
&lt;li&gt;Scope - Block Scoped in case of strict mode &amp;amp; Function Scoped is sloppy/normal mode&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Var Declarations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hoisted -✔&lt;/li&gt;
&lt;li&gt;Initial Value - undefined&lt;/li&gt;
&lt;li&gt;Scope - Function Scoped&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. let and const:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hoisted -❌&lt;/li&gt;
&lt;li&gt;Initial Value - &lt;/li&gt;
&lt;li&gt;Scope - Block Scoped&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For let and const, hoisting is not applicable so if we try to execute the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(`My name is ${name}`);
const name = 'Pawan'; // same for let

// Output: ReferenceError: Cannot access 'name' before initialization
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, for let and const declarations, we have something as the &lt;strong&gt;Temporal Dead Zone&lt;/strong&gt; which is defined as &lt;code&gt;an area of code in which a variable is defined but cannot be used&lt;/code&gt;. This is primarily because the variable has an initial value set as 'uninitialized'. The temporal dead zone lasts from &lt;code&gt;the beginning of the scope to the point where the variable is declared&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let us see the code snippet below and make it even clearer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function myFun() {
  const greeting = 'Hi';
  console.log(`${greeting} ${name}`);
  const name = 'Pawan';
}
myFun();

// Output: ReferenceError: Cannot access 'name' before initialization
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The figure below shows the Temporal Dead Zone for the snippet:&lt;br&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%2Fsqouc39kfyfzeu0ica7i.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%2Fsqouc39kfyfzeu0ica7i.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So if we try to access the variable in the Temporal Dead Zone, we get ReferenceError and this is why using let and const is recommended so as to avoid issues due to hoisting in case of var. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Function Expressions and Arrow Functions&lt;/strong&gt;:&lt;br&gt;
In case of function expressions and arrow functions, the hoisting rules depend on the type of declaration used i.e., var or let/const.&lt;/p&gt;

&lt;p&gt;This was all we had on Hoisting and The Temporal Dead Zone. In case of any queries, feel free to use the comments section.&lt;/p&gt;

&lt;p&gt;Stay Safe and Happy Learning. 🙌&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>JavaScript Behind The Scenes: Scope &amp; The Scope Chain</title>
      <dc:creator>Pawan Bhatt 👨‍💻</dc:creator>
      <pubDate>Sat, 31 Jul 2021 17:40:59 +0000</pubDate>
      <link>https://forem.com/bhattpawan/javascript-behind-the-scenes-scope-the-scope-chain-4de7</link>
      <guid>https://forem.com/bhattpawan/javascript-behind-the-scenes-scope-the-scope-chain-4de7</guid>
      <description>&lt;p&gt;Hey fellow developers, so now that we are well verse with &lt;a href="https://dev.to/bhattpawan/javascript-behind-the-scenes-the-execution-context-call-stack-4m64"&gt;The Execution Context and Call Stack&lt;/a&gt;, its now time to dive a little deeper into JavaScript and understand what The Scope and Scope Chain is.&lt;/p&gt;

&lt;h2&gt;
  
  
  What do we mean by 'Scope'?
&lt;/h2&gt;

&lt;p&gt;Scope is an environment in which a variable is accessible. It determines the &lt;em&gt;visibility&lt;/em&gt; of a variable. We cannot access a variable outside its scope. JavaScript follows &lt;strong&gt;Lexical Scoping&lt;/strong&gt; , which determines the scope of a variable by how the functions and blocks are placed in our code. Following are the different types of scopes in JavaScript:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Global Scope:&lt;/strong&gt; Variables defined in global scope are defined outside any function or block. These variables can be accessed anywhere throughout the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Function Scope:&lt;/strong&gt; Variables which are defined inside a function, have their visibility limited to inside the function. If we try to access them outside the function, we get a &lt;em&gt;ReferenceError&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Block Scope:&lt;/strong&gt; Block Scope was introduced with ES6 and only the variables defined with &lt;strong&gt;let&lt;/strong&gt; and &lt;strong&gt;const&lt;/strong&gt; have their scope limited to the block they are defined in. A block is any section of code between two curly braces {} (like in if, for and switch statements). However, the variables defined with var have their scope limited to the execution context i.e., global scope if the block is defined in global context and function scoped if the block is defined inside a function.&lt;/p&gt;

&lt;p&gt;Here is a code snippet to explain the same:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    const myName = 'Pawan Bhatt';
    // Globally Scoped, can be accessed anywhere throughout the code

    function myFunc() {
    const age = 20;
    // Function Scoped, can be accessed inside the function only
    if (age &amp;gt; 18) {
        const teenager = false;
        // Block scoped, cannot be accesses outside the if block
        var student = 'No';
        // Function scoped, can be accessed outside if but not outside myFunc()
    }
    console.log(`${myName} is aged ${age} and is teenager? ${teenager}`);
    // ReferenceError: teenager not defined
    }
    myFunc();
    console.log(student); // ReferenceError: student is not defined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Understanding the Scope Chain:
&lt;/h2&gt;

&lt;p&gt;Scope chain is what helps JavaScript to resolve the value for a variable. Scope Chain enables JavaScript to look for a particular variable or value and inherit it from the parent if it is not found in the current scope. For instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function a() {
  const x = 5;

  function b() {
    console.log(x);
    // Value picked up from parent scope
  }
  b();
}
a();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us consider the below code example and try to understand Scope Chain in a better way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const name = 'Pawan';

function first() {
  const marks = 90;

  if (marks &amp;gt; 35) {
    var result = 'Pass';
  }

  function second() {
    const rank = 1;
    console.log(`${name} has marks ${marks} and result is ${result}`);
  }
  second();
}

first();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the code snippet above, if we try to build the scope chain, it will look like:&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1627751778842%2F2DxtK47Mf.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1627751778842%2F2DxtK47Mf.png" alt="image.png"&gt;&lt;/a&gt;&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1627752862295%2Fe6lnFRvbf.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1627752862295%2Fe6lnFRvbf.png" alt="image.png"&gt;&lt;/a&gt;&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1627751695128%2FU0cc3E8lr.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1627751695128%2FU0cc3E8lr.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And the scope chain flow goes like:&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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1627752005926%2FSbjtMIBQQ.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%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1627752005926%2FSbjtMIBQQ.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This means that: &lt;strong&gt;"Every scope has access to all the variables from its outer scope"&lt;/strong&gt;.&lt;br&gt;
However, &lt;strong&gt;"The outer scope cannot access the variables defined in inner scope"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That was all we had to cover about Scope and The Scope Chain. For any queries or suggestions, feel free to use the comment section.&lt;/p&gt;

&lt;p&gt;Stay Safe and Happy Learning🙌&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript Behind The Scenes: The Execution Context &amp; Call Stack</title>
      <dc:creator>Pawan Bhatt 👨‍💻</dc:creator>
      <pubDate>Sun, 25 Jul 2021 18:09:32 +0000</pubDate>
      <link>https://forem.com/bhattpawan/javascript-behind-the-scenes-the-execution-context-call-stack-4m64</link>
      <guid>https://forem.com/bhattpawan/javascript-behind-the-scenes-the-execution-context-call-stack-4m64</guid>
      <description>&lt;p&gt;Hey fellow developers, in the last blog, we learnt about &lt;a href="https://dev.to/bhattpawan/discovering-javascript-the-javascript-engine-runtime-5hia"&gt;&lt;strong&gt;The JavaScript Engine &amp;amp; The Runtime&lt;/strong&gt;&lt;/a&gt; . The topic for today will be &lt;strong&gt;The Execution Context and Call Stack&lt;/strong&gt;. So without much of an ado, lets begin.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an Execution Context?
&lt;/h2&gt;

&lt;p&gt;Execution Context is an environment that executes our JavaScript Code. In addition to this, it also stores all the necessary information that is needed for execution like the variable names or the argument names passed to a function. &lt;br&gt;
&lt;em&gt;"Everything in JavaScript happens inside the Execution Context"&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Execution Context made easy....
&lt;/h3&gt;

&lt;p&gt;To understand Execution Context in a better way, let us take an example of us ordering a burger from our favorite store. So, when we get our burger, we get the following things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Burger (of course)&lt;/li&gt;
&lt;li&gt;The Polybag / Paperbag (containing our order)&lt;/li&gt;
&lt;li&gt;Ketchup, Tissues, Fries, Cola and other add-ons, which enhance our burger eating experience.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So considering our order, we can draw the following comparisons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Burger -&amp;gt; Our JavaScript Code&lt;/li&gt;
&lt;li&gt;The Polybag / Paperbag -&amp;gt; Execution Context &lt;/li&gt;
&lt;li&gt;Ketchup, Tissues, Fries etc -&amp;gt; The functions, variables or arguments passed to the function which help/enhance our JavaScript code execution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an illustration for the same:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3E2au8V8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627029139394/0p5ZR0kUv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3E2au8V8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627029139394/0p5ZR0kUv.png" alt="image.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  What's inside an Execution Context?
&lt;/h3&gt;

&lt;p&gt;An Execution Context primarily consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variable Environment: The variable environment consists of the following:

&lt;ul&gt;
&lt;li&gt;let, const and var declarations&lt;/li&gt;
&lt;li&gt;functions&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Scope Chain (will be covered later)&lt;/li&gt;
&lt;li&gt;this keyword (will be covered later)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, for now, lets just forget that we know anything like the scope chain &amp;amp; this keyword (we'll cover it later, don't worry), and focus on the variable environment.&lt;/p&gt;

&lt;p&gt;If we consider code example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const name = 'Hello World';

function sayHello(){
console.log('Hello People');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Execution Context will look like:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kzY66XCa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8wx71tja8u6wctwqv7n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kzY66XCa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8wx71tja8u6wctwqv7n.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
Before moving on to how the JavaScript code is executed, let us have a look at what the &lt;strong&gt;Call Stack&lt;/strong&gt; is.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Call Stack:
&lt;/h2&gt;

&lt;p&gt;The Call Stack is a place that stacks execution context on top of each other in order to keep track of the execution order. The Execution Contexts are processed in  LIFO manner i.e., Last-In-First-Out.&lt;br&gt;
Here is an illustration:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UeF6NHYy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1cvfer1foy0yllfl218l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UeF6NHYy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1cvfer1foy0yllfl218l.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How is the code &lt;strong&gt;actually&lt;/strong&gt; Executed?
&lt;/h2&gt;

&lt;p&gt;So now that we are aware of the terminologies, let us see how our code gets executed. Take the following code snippet for instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const name = 'Pawan';

function sum(a, b){
let s = a + b;
return s;
}
const getSum = sum(2, 3);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code execution takes place mainly in two phases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The Creation Phase:&lt;/strong&gt; In which the variables, functions are declared in the variable environment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Execution Phase:&lt;/strong&gt; In which the code is executed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;1. The Creation Phase&lt;/strong&gt;&lt;br&gt;
During the creation phase, the variables and functions defined in global scope are allocated memory and assigned an initial value. For the code snippet above, the global execution context in creation phase will look like.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aaa98a97--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dubi9peyuif0yq6kqcp4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aaa98a97--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dubi9peyuif0yq6kqcp4.png" alt="image"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. The Execution Phase:&lt;/strong&gt;&lt;br&gt;
During the execution phase, the variables are assigned values and the code is executed &lt;em&gt;one-line-at-a-time&lt;/em&gt;. The execution context changes as follows:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6PQQIaQl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ezf22j5djzpq88wqgek.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6PQQIaQl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ezf22j5djzpq88wqgek.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6UNbDTRr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yo7orefrwqp4ic71py20.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6UNbDTRr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yo7orefrwqp4ic71py20.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So now you must be thinking, for multiple functions we would have hundreds of execution contexts, so how would JavaScript track its execution? The answer is &lt;strong&gt;The Call Stack&lt;/strong&gt;. Below is how the call stack will be populated for our code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When the code is run for the first time, the call stack looks like:&lt;br&gt;
&lt;strong&gt;Creation Phase&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EzUB7BKb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0u4b2ge5vesxxdoa9ug.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EzUB7BKb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0u4b2ge5vesxxdoa9ug.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Execution Phase&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0sqefyrd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e459ehu5ec5jxq8lju3a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0sqefyrd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e459ehu5ec5jxq8lju3a.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When the function, sum gets called, it gets its own execution context and now our call stack looks like:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Creation Phase:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_ctttz6z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nppdqxfcgy6jy2iwi7wb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_ctttz6z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nppdqxfcgy6jy2iwi7wb.png" alt="image"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Execution Phase:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fNFaOTqE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1xb07bmcceiesog2f4kx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fNFaOTqE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1xb07bmcceiesog2f4kx.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now that our function execution context has completed its execution, it gets removed from the call stack ( for now lets suppose that it does) and our getSum variable gets populated. So now our call stack will be:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---fHXa9hr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bv930yuett697aj6bky7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---fHXa9hr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bv930yuett697aj6bky7.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After our global execution context is done executing all the statements, it will just remain like that waiting for any explicit calls/instructions including the click events or hover events etc. The execution context remains active so long as our browser window is active.&lt;/p&gt;

&lt;p&gt;So, I hope I was able to explain how the Execution Context and the Call Stack works together to execute our JavaScript code. To summarize it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Execution Context executes the JavaScript code.&lt;/li&gt;
&lt;li&gt;Call Stack maintains a stack of execution contexts to maintain an order of execution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's all for now. Any queries and recommendations can be posted in the comment box below.&lt;/p&gt;

&lt;p&gt;Stay Safe &amp;amp; Happy Learning 🙌.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript Behind The Scenes: The JavaScript Engine &amp; Runtime</title>
      <dc:creator>Pawan Bhatt 👨‍💻</dc:creator>
      <pubDate>Sat, 17 Jul 2021 16:25:33 +0000</pubDate>
      <link>https://forem.com/bhattpawan/discovering-javascript-the-javascript-engine-runtime-5hia</link>
      <guid>https://forem.com/bhattpawan/discovering-javascript-the-javascript-engine-runtime-5hia</guid>
      <description>&lt;p&gt;Hey fellow developers, so as I started learning JavaScript, I came across some really important topics that every JavaScript developer must know. So in the "JavaScript Behind The Scenes" series, we will be covering these &lt;strong&gt;important&lt;/strong&gt; topics. So without any further delay, let's jump right in.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a JavaScript Engine?
&lt;/h2&gt;

&lt;p&gt;JavaScript Engine is nothing but a program that executes JavaScript code. Every browser has its own JavaScript engine. Some of the well known JavaScript engines are V8, SpiderMonkey, Chakra etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Components of a JavaScript Engine
&lt;/h2&gt;

&lt;p&gt;Every JavaScript Engine has two components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Call Stack&lt;/strong&gt;: It is the portion of JavaScript Engine where our code is actually executed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Heap&lt;/strong&gt;: The heap section of JavaScript Engine is a memory pool that stores all the objects which out application needs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below is a diagram to depict the same:&lt;br&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%2Frz2bbtnfcnde5walng6j.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%2Frz2bbtnfcnde5walng6j.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How is the code compiled to Machine Code?
&lt;/h2&gt;

&lt;p&gt;JavaScript uses &lt;em&gt;Just-in-time-compilation&lt;/em&gt; technique to execute the code which mainly consists of &lt;em&gt;converting all of the code into machine code at once and then executing the code immediately&lt;/em&gt;. &lt;br&gt;
So, it is a hybrid between compilation and interpretation (both the concepts are explained in detail later). For now, just note that in compilation, all the code is converted into machine code at once and is then executed later when needed. However, in interpretation the interpreter converts and executes each line of code simultaneously. Below diagram illustrates the &lt;em&gt;just-in-time&lt;/em&gt; execution procedure:&lt;br&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%2Fov93mydg8dchj07b4unn.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%2Fov93mydg8dchj07b4unn.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps involved in 'Just-In-Time-Compilation' of JavaScript
&lt;/h2&gt;

&lt;p&gt;Following are the steps involved in JavaScript's JIT compilation when any piece of JS code executes in JavaScript Engine:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Parsing&lt;/strong&gt;: Parsing means &lt;em&gt;reading the code&lt;/em&gt;. During this process, the code is parsed and converted to a &lt;em&gt;AST(Abstract Syntax Tree)&lt;/em&gt;. This is done by splitting the code into small meaningful pieces and then saving them all in the form of a tree. This is the step where syntactical errors are checked. This AST is later used to generate the machine code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Compilation&lt;/strong&gt;: In this step, the generated AST is compiled to a machine code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Execution&lt;/strong&gt;: The generated machine code is executed immediately.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Below figure depicts the process:&lt;br&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%2Fysb9tpl4rptxmec9nyp9.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%2Fysb9tpl4rptxmec9nyp9.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: What is the difference between Compilation and Interpretation?
&lt;/h2&gt;

&lt;p&gt;Both Compilation and Interpretation convert our source code to machine code, however the way in which they do it differs. So here goes the detailed explanation for the same.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compilation:
&lt;/h3&gt;

&lt;p&gt;In compilation, the entire source code is converted into machine code at once, and is written into a file that can be executed by the computer. After the file is created, there is no need of the source code to run the code, the file created after compilation is used for running the code. In case of compilation, the code execution can take place way after the code has been compiled. For instance, when we run any application on our system, the file we run is actually an executable file that is made after compiling the source code required to perform the operation which our application is performing. Also, you don't even know when the file (say the .exe file) was actually compiled. It may have been compiled an year ago but still can be executed after such a long time. The below diagram explains compilation process:&lt;br&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%2Fmp7u558jptnbnemlczpp.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%2Fmp7u558jptnbnemlczpp.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Interpretation:
&lt;/h3&gt;

&lt;p&gt;An Interpreter, works differently as compared to a compiler. Instead of compiling all the code at once, it executes the source code line by line. So that means, we do not have any concept of &lt;em&gt;intermediate file creation&lt;/em&gt; here. Simply put forward, it just takes the source code &lt;em&gt;one-line-at-a-time&lt;/em&gt;, and executes the code i.e., performs the function that the code is suppose to perform. So in interpretation, we will require the source code every time we need to run our code. Following is the diagram of how a interpretation works:&lt;br&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%2Fhlsl93hx9xahht7dpiqr.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%2Fhlsl93hx9xahht7dpiqr.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So that's pretty much it about the JavaScript Engine &amp;amp; Runtime. We will cover more such interesting topics in upcoming blogs.&lt;/p&gt;

&lt;p&gt;Stay Safe &amp;amp; Happy Learning 🙌.&lt;/p&gt;

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