<?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: therealsuhail</title>
    <description>The latest articles on DEV Community by therealsuhail (@suhail).</description>
    <link>https://dev.to/suhail</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%2F711077%2F9c7d815f-06de-4f4f-8246-add39e83a57e.png</url>
      <title>DEV Community: therealsuhail</title>
      <link>https://dev.to/suhail</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/suhail"/>
    <language>en</language>
    <item>
      <title>Hoisting in JS</title>
      <dc:creator>therealsuhail</dc:creator>
      <pubDate>Mon, 27 Jun 2022 15:26:06 +0000</pubDate>
      <link>https://dev.to/suhail/hoisting-in-js-oj8</link>
      <guid>https://dev.to/suhail/hoisting-in-js-oj8</guid>
      <description>&lt;p&gt;Hey geeks, today let's read about hoisting in JS.&lt;/p&gt;

&lt;p&gt;Hoisting is one of those fundamentals which i feel every JS developer should know.&lt;br&gt;
In simple terms, hoisting allows you to use functions and variables before they are declared.&lt;br&gt;
Let us look at the code 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(name)
var name = 'hello JS'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, JS will not throw any error rather it outputs 'undefined'. This is what hoisting is all about. It happens since JS allocates memory to variables and functions followed by the execution. In the memory allocation phase, variable &lt;strong&gt;name&lt;/strong&gt; is being initialized with undefined , that is why it does not throw any error. Knowing about &lt;strong&gt;execution context&lt;/strong&gt; will help you understand hoisting in a better way. You can check my article on execution context &lt;a href="https://dev.to/suhail/behind-the-scenes-in-js-1gbm"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variable hoisting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Variable hoisting behaves differently depending on how the variables are declared. Let us take a look at the code 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(testVar)
let testVar = 'hello JS'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is quite surprising that it throws a reference error unlike var. const also exhibits the same behaviour. we get such errors when let and const are in &lt;strong&gt;temporal dead zone&lt;/strong&gt;. Temporal dead zone is nothing but the time between the variable is declared and assigned value.&lt;/p&gt;

&lt;p&gt;let and const were introduced in ES5 to avoid using variables before their declaration. It is to be noted that interpreter hoists &lt;strong&gt;let&lt;/strong&gt; and &lt;strong&gt;const&lt;/strong&gt;, but in some other memory space, whereas in case of &lt;strong&gt;var&lt;/strong&gt; it is hoisted in global execution context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Function hoisting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Functions are also hoisted in the same way like variables. Let us take a look at 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;getName()
function getName() {
    console.log("function")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here it outputs 'function' since &lt;strong&gt;getName&lt;/strong&gt; is allocated memory before execution.&lt;/p&gt;

&lt;p&gt;let us look at another example&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Here you will be surprised to see that it throws reference error unlike the previous example. It is because 'getName' behaves like a variable here. Only function declarations are hoisted and not function statements.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Behind the scenes in JS</title>
      <dc:creator>therealsuhail</dc:creator>
      <pubDate>Wed, 06 Oct 2021 14:20:01 +0000</pubDate>
      <link>https://dev.to/suhail/behind-the-scenes-in-js-1gbm</link>
      <guid>https://dev.to/suhail/behind-the-scenes-in-js-1gbm</guid>
      <description>&lt;p&gt;Hey everyone. Hope you all are doing great. This is my first blog and I hope that you will like and share it among your tech buddies.&lt;/p&gt;

&lt;p&gt;Today I am going to walk you through &lt;strong&gt;Execution Context&lt;/strong&gt; in JS.I have been coding in JavaScript for the past few months and I found out that many people miss out on this topic. Especially if you are a beginner, this is a must read.This topic is a base for learning advanced topics like hoisting, closure and scope chain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Execution context&lt;/strong&gt; is an environment where everything happens in JS. All your memory allocations and code executions.&lt;/p&gt;

&lt;p&gt;There are 3 types of execution contexts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Global execution context (GEC)&lt;/strong&gt; – GEC is where all your global variables and functions are stored.There can not be more than one GEC in JS as it is a single threaded language.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Functional execution context (FEC)&lt;/strong&gt; – FEC is created each time a function is called in JS. There can be ‘n’ number of FECs, since there can be many functions called from a JS file.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Eval&lt;/strong&gt; : Execution context inside eval function.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Execution context has a stack data structure where it follows a last in first out(LIFO) data structure.Always GEC will be pushed first followed by FECs.Each FEC will be popped once it is fully executed.&lt;br&gt;
You can refer the figure below for more clarity on this.&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%2Fowi3r21q7n3l7eyeevbh.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%2Fowi3r21q7n3l7eyeevbh.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
As each function gets executed , it gets popped out of the stack.&lt;/p&gt;

&lt;p&gt;Execution context in JavaScript engine happens in 2 steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Creation phase&lt;/strong&gt; – this is the phase where memory is allocated to all your variables and functions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code execution phase&lt;/strong&gt; – this is the phase where our code logic is executed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Don’t worry if you did not get what I wrote above, I will help you understand with an example.You just have to keep reading for that😃.&lt;/p&gt;

&lt;p&gt;Let's look at this simple program of adding two numbers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 10
let b = 20
function add(a, b){
   let num1 = a
   let num2 = b
   return num1+num2
}
let result = add(a, b)
console.log(result)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We all know that the output is "30".But let's dive in to know what actually happened to get us this output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First phase&lt;/strong&gt; is where we allocate memory to all the variables and functions in GEC. It will look something like this:&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%2Fgqfhar43pig1r3dxdl62.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%2Fgqfhar43pig1r3dxdl62.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GEC is then pushed inside the stack.&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%2Fy0gofdneb9l3xizd2l8u.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%2Fy0gofdneb9l3xizd2l8u.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Second phase&lt;/strong&gt; is where the code starts to execute and starts assigning values to those variables we created. It is to be noted here that every time a function is called, it creates a new execution context.&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%2F213hp8unbyllddjtgao4.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%2F213hp8unbyllddjtgao4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As the code starts to execute, variables '&lt;strong&gt;a&lt;/strong&gt;' and '&lt;strong&gt;b&lt;/strong&gt;' gets assigned their respective values and when function '&lt;strong&gt;add&lt;/strong&gt;' is called, it creates a new execution context.This function execution context is then pushed into the stack.&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%2F7v3oufm1xnlkd0otbplu.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%2F7v3oufm1xnlkd0otbplu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;once the code in function "&lt;strong&gt;add&lt;/strong&gt;" gets executed, it returns the value '&lt;strong&gt;30&lt;/strong&gt;' and function execution context is removed and popped out of the stack.&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%2F4eiwizrmb4d5l8txso5s.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%2F4eiwizrmb4d5l8txso5s.png" alt="Alt Text"&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv83wociyl60beo99cirj.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%2Fv83wociyl60beo99cirj.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once JS executes the last line .i.e printing the '&lt;strong&gt;result&lt;/strong&gt;' global execution context gets removed and hence popped out of the stack.&lt;br&gt;
This is what happens when you execute every JS program. Hope you would remember execution context when you write your next JS program.&lt;/p&gt;

&lt;p&gt;Please feel free to give your comments below. I wanted to show you the full flow by using debugger , but it will make this article too long.May be i will include that in the next post😃.&lt;/p&gt;

&lt;p&gt;Thank you&lt;/p&gt;

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