<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Sushant Mahalle]]></title><description><![CDATA[Sushant Mahalle]]></description><link>https://sushantmahalle.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 30 Aug 2026 16:27:22 GMT</lastBuildDate><atom:link href="https://sushantmahalle.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Array methods Every JS developer must know.]]></title><description><![CDATA[Intro
An array is a widely used data structure in the JavaScript language.
An array is a list-like data structure consisting of a collection of elements. Each element is determined by its index. The count of the index start's from 0 and the last inde...]]></description><link>https://sushantmahalle.hashnode.dev/array-methods-every-js-developer-must-know</link><guid isPermaLink="true">https://sushantmahalle.hashnode.dev/array-methods-every-js-developer-must-know</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[array methods]]></category><category><![CDATA[learning]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[SUSHANT CHANDRASHEKHAR MAHALLE]]></dc:creator><pubDate>Tue, 14 Sep 2021 14:56:21 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1631631248326/fsMiSGGkV.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h1 id="intro">Intro</h1>
<p>An array is a widely used data structure in the JavaScript language.</p>
<p>An array is a list-like data structure consisting of a collection of elements. Each element is determined by its index. The count of the index start's from 0 and the last index decides the length of an array.</p>
<p>While learning I encountered a lot of places where the array was implemented and JavaScript array methods made a lot of things easier. Array provides plenty of methods. So I decided to write a blog about it. Documenting what I Have learned.</p>
<p>All this content is freely available to read on the internet. I referred to the <a target="_blank" href="https://developer.mozilla.org/en-US/">MDN Web docs</a> for understanding.</p>
<h1 id="functions">Functions</h1>
<h2 id="push"><code>push()</code> -</h2>
<p>The push() method is used to add one or more elements to the end of the array. This method changes the length of an array.</p>
<ul>
<li><h3 id="parameters">parameters :</h3>
<p>the element(s) that are added to the end of an array.</p>
</li>
<li><h3 id="return">return :</h3>
<p>the new length of an array.</p>
</li>
</ul>
<h2 id="pop"><code>pop()</code> -</h2>
<p> The pop() method removes only the last element in an array. </p>
<ul>
<li><h3 id="return">return :</h3>
the removed element.</li>
</ul>
<pre><code class="lang-jsx">&gt;<span class="hljs-keyword">let</span> tech=[<span class="hljs-string">'html'</span>,<span class="hljs-string">'css'</span>]

&gt;tech.push(<span class="hljs-string">'JS'</span>)
&lt;(<span class="hljs-number">3</span>) <span class="hljs-comment">//returns new length ['html','css','JS']</span>
&gt;tech.push(<span class="hljs-string">'react'</span>,<span class="hljs-string">'angular'</span>) <span class="hljs-comment">//adding more than one element</span>
&lt;(<span class="hljs-number">5</span>) <span class="hljs-comment">//['html', 'css', 'JS', 'react', 'angular']</span>

&gt;<span class="hljs-keyword">let</span> tech=[<span class="hljs-string">'html'</span>, <span class="hljs-string">'css'</span>, <span class="hljs-string">'JS'</span>, <span class="hljs-string">'react'</span>, <span class="hljs-string">'angular'</span>]
&gt;tech.pop()
&lt; <span class="hljs-string">'angular'</span> <span class="hljs-comment">// return the removed element</span>
</code></pre>
<h2 id="shift"><code>shift()</code> -</h2>
<p>The shift() method removes the first element from an array. This method changes the length of an array. It removes the 0th element and shifts the indexes of all consecutive elements down.</p>
<ul>
<li><h3 id="return">return :</h3>
the removed element</li>
</ul>
<h2 id="unshift"><code>unshift()</code> -</h2>
<p> The unshift() method add one or more elements to the start of an array. If more than one element is added then it is added as a chunk to the start.</p>
<ul>
<li><h3 id="parameters">parameters :</h3>
<p>the elements to add at the front of an array.</p>
</li>
<li><h3 id="return">return :</h3>
<p>new length of array</p>
</li>
</ul>
<pre><code class="lang-jsx">&gt;<span class="hljs-keyword">let</span> arr1=[<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>]
&gt;arr1.shift()
&lt; <span class="hljs-number">3</span> <span class="hljs-comment">//returns 1</span>
<span class="hljs-comment">//arr1=[4,5,6]</span>

&gt;arr1.unshift(<span class="hljs-number">3</span>)
&lt; <span class="hljs-number">4</span> <span class="hljs-comment">//returns the new length</span>
<span class="hljs-comment">//arr1 = [3, 4, 5, 6]</span>

&gt;arr1.unshift(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>) <span class="hljs-comment">//adding more than 1 elements</span>
&lt; <span class="hljs-number">6</span> <span class="hljs-comment">//returns the new length</span>
<span class="hljs-comment">//arr1=[1, 2, 3, 4, 5, 6]</span>
</code></pre>
<h2 id="reverse"><code>reverse()</code> -</h2>
<p> The reverse() as the name suggest's reverses an array. The original array is changed.</p>
<ul>
<li><h3 id="return">return :</h3>
reversed array</li>
</ul>
<pre><code class="lang-jsx"><span class="hljs-keyword">let</span> arr=[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
<span class="hljs-keyword">let</span> revArr=arr.reverse()
<span class="hljs-comment">//revArr = [5,4,3,2,1]</span>
</code></pre>
<h2 id="join"><code>join()</code> -</h2>
<p>The join() method creates and returns a string concatenating all the elements of an array it is called upon.</p>
<ul>
<li><h3 id="parameters-optional">parameters [optional] :</h3>
<p>separator that concatenates all array element, if nothing is passed the array elements are separated with a comma (",") </p>
</li>
<li><h3 id="return">return :</h3>
<p>The string with all array elements joined</p>
</li>
</ul>
<pre><code class="lang-jsx"><span class="hljs-keyword">let</span> letters=[<span class="hljs-string">'s'</span>,<span class="hljs-string">'u'</span>,<span class="hljs-string">'s'</span>,<span class="hljs-string">'h'</span>,<span class="hljs-string">'a'</span>,<span class="hljs-string">'n'</span>,<span class="hljs-string">'t'</span>]
<span class="hljs-keyword">let</span> joinedLetters=array.join()
<span class="hljs-comment">//joinedLetters = 's,u,s,h,a,n,t' //returns string joined with (",")</span>
<span class="hljs-keyword">let</span> joinedLetters2=array.join(<span class="hljs-string">"-"</span>)
<span class="hljs-comment">//joinedLetters2 = 's-u-s-h-a-n-t' //returns string joined with separator ("-")</span>
</code></pre>
<h2 id="slice"><code>slice()</code> -</h2>
<p>The slice method creates a shallow copy of part of the array into a new array-like object from <code>start</code> and <code>end</code> values passed as parameters. The original array is unchanged.</p>
<ul>
<li><h3 id="parameters">parameters :</h3>
<p>takes two parameters and both are optional. The first is a <code>start</code>, it's the starting index from where to start extraction. Another is <code>end</code> the index where to end the extraction. The element at <code>end</code> index is not included in the new array. The negative index can also be passed here. If the <code>end</code> is not present then the extraction goes on till the last index.</p>
</li>
<li><h3 id="return">return :</h3>
<p>new array object containing the extracted elements.</p>
</li>
</ul>
<pre><code class="lang-jsx"><span class="hljs-keyword">let</span> itemInBag= [<span class="hljs-string">"apple"</span>,<span class="hljs-string">"oranges"</span>,<span class="hljs-string">"banana"</span>,<span class="hljs-string">"cherry"</span>,<span class="hljs-string">"cabbage"</span>,<span class="hljs-string">"spinach"</span>,<span class="hljs-string">"milk"</span>,<span class="hljs-string">"butter"</span>]
<span class="hljs-keyword">let</span> fruits=itemInBag.slice(<span class="hljs-number">0</span>,<span class="hljs-number">4</span>) <span class="hljs-comment">//starts extraction from 0th index and ends at 4th(done not include end index)</span>
<span class="hljs-comment">//fruits = ['apple', 'oranges', 'banana', 'cherry']</span>

<span class="hljs-keyword">let</span> dairy=itemInBag.slice(<span class="hljs-number">-2</span>) <span class="hljs-comment">//extracts the last to index</span>
<span class="hljs-comment">//dairy = ['milk', 'butter']</span>

<span class="hljs-keyword">let</span> veggies=itemInBag.slice(<span class="hljs-number">-4</span>,<span class="hljs-number">-2</span>) <span class="hljs-comment">//extract last 4th to last 2nd(doesn't include 2nd) index</span>
<span class="hljs-comment">//veggies=['cabbage', 'spinach']</span>
</code></pre>
<h1 id="higher-order-functions">Higher-Order Functions</h1>
<p>While learning JavaScript we may have come across the term "higher-order functions".These functions are used heavily in JavaScript, which makes it suitable for functional programming. It may sound like a really complex thing to understand, but it isn't.</p>
<p>Higher-Order Functions are functions that operate on other functions, either by taking them as arguments or returning them.</p>
<p>Let's discuss some of the most used functions:</p>
<h2 id="map"><code>map()</code> -</h2>
<p>The map() method transforms an array by applying a function to its elements and building a new array from its returned values. The map function will take returned values from callback functions and create a new array. The new array created has the same length. The original array remains unchanged.</p>
<ul>
<li><h3 id="parameters-callback-function">parameters (callback function) :</h3>
</li>
</ul>
<p><strong>currentValue</strong> : The current element being processed.</p>
<p><strong>currentIndex[optional]</strong> : The index of the current element.</p>
<p><strong>array [optional]</strong> : The array map is used upon.</p>
<ul>
<li><h3 id="return">return :</h3>
a new array with each array being the result of a callback function.</li>
</ul>
<pre><code class="lang-jsx"><span class="hljs-keyword">let</span> arr=[<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>]
<span class="hljs-keyword">let</span> doubleArr=arr.map(<span class="hljs-function"><span class="hljs-params">elem</span> =&gt;</span> elem*<span class="hljs-number">2</span> ) <span class="hljs-comment">//callback function to get double of each element</span>
<span class="hljs-comment">//arr = [2, 4, 6, 8, 10]</span>
</code></pre>
<h2 id="filter"><code>filter()</code> -</h2>
<p>The filter() method finds all the elements that satisfy the condition or of same type and build a new array out of it. Each element is tested by a callback function. Rather than deleting the elements from original array  based on return value from callback function it builds a new array.</p>
<ul>
<li><h3 id="parameters-callback-function">parameters (callback function) :</h3>
</li>
</ul>
<p><strong>currentValue</strong> : The current element being processed.</p>
<p><strong>currentIndex[optional]</strong> : The index of current element being processed.</p>
<p><strong>array [optional]</strong> : The array <code>filter()</code> is called upon.</p>
<ul>
<li><h3 id="return">return -</h3>
A new array that passes the test(callback function). An empty array is returned if no element pass the test.</li>
</ul>
<pre><code class="lang-jsx"><span class="hljs-keyword">let</span> students = [{<span class="hljs-attr">id</span>:<span class="hljs-number">1</span>,<span class="hljs-attr">dept</span>:<span class="hljs-string">"CS"</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">"Sachin"</span>} , {<span class="hljs-attr">id</span>:<span class="hljs-number">2</span>,<span class="hljs-attr">dept</span>:<span class="hljs-string">"MECH"</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">"Ram"</span>},{<span class="hljs-attr">id</span>:<span class="hljs-number">3</span>,<span class="hljs-attr">dept</span>:<span class="hljs-string">"CS"</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">"Virat"</span>},{<span class="hljs-attr">id</span>:<span class="hljs-number">4</span>,<span class="hljs-attr">dept</span>:<span class="hljs-string">"ENTC"</span>,<span class="hljs-attr">name</span>:<span class="hljs-string">"Sushant"</span>}]
<span class="hljs-keyword">let</span> filteredDept=students.filter(<span class="hljs-function"><span class="hljs-params">student</span>=&gt;</span> student.dept==<span class="hljs-string">"CS"</span>) <span class="hljs-comment">//filtering based on dept="CS"</span>
<span class="hljs-comment">//filteredDept = [{id: 1, dept: 'CS', name: 'Sachin'},{id: 3, dept: 'CS', name: 'Virat'}]</span>

<span class="hljs-keyword">let</span> filterName=students.filter(<span class="hljs-function"><span class="hljs-params">student</span> =&gt;</span> student.name.length&gt;<span class="hljs-number">6</span>) <span class="hljs-comment">//filter whose name length&gt;6</span>
<span class="hljs-comment">//filterName = [{id: 4, dept: 'ENTC', name: 'Sushant'}]</span>
</code></pre>
<h2 id="reduce"><code>reduce()</code> -</h2>
<p>The reduce() method applies reducer across all element of array and returns a single value. It returns a single value calculated on each element of array. The reducer is initialized and processed throughout the array, at each step calculates the result and adds to previous steps result till last index of array.</p>
<ul>
<li><h3 id="parameters">parameters :</h3>
</li>
</ul>
<h4 id="callback-function">callback function:</h4>
<ul>
<li><strong>prevoiusValue</strong> - The resulting value from last callback function i.e. Accumulator</li>
<li><strong>currentValue</strong> - The value of current element.</li>
<li><strong>currentIndex</strong> - The index of current element.</li>
<li><strong>array [optional]</strong> - The array <code>reduce()</code> is applied upon.</li>
</ul>
<p><strong>initialValue [optional]</strong> -</p>
<ul>
<li>If it is provided, then previousValue will be equal to initialValue and currentValue equal to first element.</li>
<li><p>If it is not provided, then previousValue is set to first first element and currentValue is the second element of array.</p>
</li>
<li><h3 id="return">return :</h3>
<p>The value returned after running reducer function on all array elements.</p>
</li>
</ul>
<pre><code class="lang-jsx"><span class="hljs-keyword">let</span> report={ <span class="hljs-attr">id</span>:<span class="hljs-number">1</span>,
             <span class="hljs-attr">marks</span>:[<span class="hljs-number">25</span>,<span class="hljs-number">32</span>,<span class="hljs-number">44</span>,<span class="hljs-number">38</span>,<span class="hljs-number">29</span>,<span class="hljs-number">41</span>],
           }
<span class="hljs-keyword">let</span> totalMarks=report.marks.reduce(<span class="hljs-function">(<span class="hljs-params">previousValue,currentValue</span>) =&gt;</span> previousValue+currentValue,<span class="hljs-number">0</span>)
<span class="hljs-comment">//returns total of all elements of array, initialValue is set to 0</span>
report[<span class="hljs-string">'total'</span>]=totalMarks
<span class="hljs-comment">//report={ id:1,marks:[25,32,44,38,29,41],total:209}</span>
</code></pre>
<h1 id="conclusion">Conclusion</h1>
<p>Before we end, I hope you got an insight into the array method that makes our programming easy. To get a better grip over the concept we need to solve some problems and practice them.</p>
<p>Other than this <code>concat()</code>,<code>sort()</code>,<code>find()</code>,<code>findIndex()</code>,<code>fill()</code>,<code>forEach()</code>,<code>some()</code>,<code>every()</code> ... are some  very useful array method.</p>
<p>Let's connect. You will find me active on Twitter<a target="_blank" href="https://twitter.com/MasterThinking">(@MasterThinking)</a>. Please feel free to give a follow.</p>
]]></content:encoded></item><item><title><![CDATA[Javascript under the hood feat. Execution Context and Call stack]]></title><description><![CDATA[Hello there 🖐
By the end of this blog, you will understand how is JS code executed in the browser. 
We will talk about the following topics:

Execution Context

Call Stack


So, let's get started and discuss it.
Execution Context
Every JS file has s...]]></description><link>https://sushantmahalle.hashnode.dev/javascript-under-the-hood-feat-execution-context-and-call-stack</link><guid isPermaLink="true">https://sushantmahalle.hashnode.dev/javascript-under-the-hood-feat-execution-context-and-call-stack</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[Foundation]]></category><category><![CDATA[Beginner Developers]]></category><category><![CDATA[100DaysOfCode]]></category><category><![CDATA[Learning Journey]]></category><dc:creator><![CDATA[SUSHANT CHANDRASHEKHAR MAHALLE]]></dc:creator><pubDate>Fri, 11 Jun 2021 15:17:25 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1623424682404/XrNTLQgAN.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hello there 🖐</p>
<p>By the end of this blog, you will understand how is JS code executed in the browser. 
We will talk about the following topics:</p>
<ul>
<li><p>Execution Context</p>
</li>
<li><p>Call Stack</p>
</li>
</ul>
<p>So, let's get started and discuss it.</p>
<h1 id="execution-context">Execution Context</h1>
<p>Every JS file has several lines of code, organized with the help of variables, data structures, functions, and many more. The code is straightforward. However, behind the scene, JavaScript does many things. </p>
<p>We all must have heard that </p>
<blockquote>
<p>Everything in JavaScript happens inside an Execution Context.</p>
</blockquote>
<p>But what is this Execution Context? Let me help to make it simple for you.
A Lexical Environment determines how and where we write our code physically. Note that there is more than one Lexical Environment in code, but not all Lexical Environment get executed at once. When the JS engine executes a code, it creates an execution context. The execution context is the environment where a specific portion of the code executes. </p>
<p>Each execution context has 2 phases - <strong>the memory creation phase and the execution phase</strong>. These might seem some heavy topics but actually, they are pretty easy to learn.</p>
<p>Let me make you absorb this. Think it in a way that our code may or may not be written in a sequence. Some portion may in at the start of the code, or at the very end, or somewhere in the middle, but the actual sequence of code is in what order it is required or called by the compiler or engine. The order of lexically written code and order of execution is not the same. The order in which the code is executed/run by the system is Execution Context.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623420130503/wqcM4cwLl.png" alt="Execution Context.png" /></p>
<h2 id="global-execution-context">Global Execution Context</h2>
<p>When a script is executed the first time, the Global execution context is created by Javascript Engine. It has 2 phases</p>
<ol>
<li>Memory Creation Phase</li>
<li>Execution phase</li>
</ol>
<h3 id="creation-phase">Creation Phase</h3>
<p>In the creation phase, two unique things get created:</p>
<ul>
<li>A global object called <strong>WINDOW</strong>( in browser).</li>
<li>A global variable called <strong>THIS</strong>.</li>
</ul>
<p>Now let's figure out what happens when we open and execute JS files in a browser. Now here is the twist, even when we Execute an empty file Execution context is generated. I will try to explain this with the help of the below example.</p>
<h3 id="the-shortest-javascript-program-everfile-is-empty">The shortest Javascript program ever.(<strong>File is Empty </strong>)</h3>
<ol>
<li>The Global Execution Context gets created when we load the JavaScript file, even when it is <strong>empty.</strong></li>
<li>It creates 2 special things -<ul>
<li><strong>window </strong>object </li>
</ul>
</li>
</ol>
<p>value of <code>window</code>
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623419065613/N_Y3GllE-.png" alt="value of window.png" /> </p>
<ul>
<li><strong>this</strong></li>
</ul>
<p>value of <code>this</code></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623419315948/kLSu0iMic.png" alt="value of this.png" /></p>
<ol>
<li>For an empty file <code>window</code> and <code>this</code> are equal.<blockquote>
<p><code>window===this 
//true</code></p>
</blockquote>
</li>
<li>As the file is empty, there is <strong>no </strong>execution phase here.</li>
</ol>
<h3 id="when-executes-a-file-with-variable-and-functions">When executes a file with variable and functions:</h3>
<p>Wherever variables are declared, <strong>memory is created</strong>  for the following variable.
Variable gets initialized with a unique value <code>undefined</code>. There is a difference between <code>not defined</code> and <code>undefined</code>. here the variables are given memory but no value is defined to it, that's why <code>undefined</code>.</p>
<p>If there is a function, then it gets <strong>placed directly into the memory</strong>, consider that the function copied as it is in memory. For every function in the program, a separate Function Execution Context(FEC) is created. We will talk about it below. When FEC is created, the control is passed to the newly created FEC.</p>
<h3 id="execution-phase">Execution Phase</h3>
<p>The code execution starts in this phase. During the execution phase, the JavaScript engine executes the code line by line. Values are assigned to variables and functions are called. The call stack, which is empty now has GEC at its top as it is pushed when the execution starts.</p>
<blockquote>
<p>Remember, during the creation phase functions are already stored in the memory.</p>
</blockquote>
<p>Now for every function call, the javascript engine creates another Function Execution Context separately. Always keep in mind that </p>
<blockquote>
<p>Javascript is a "synchronous single-threaded" language. 
So not more than one execution can happen at a given time.</p>
</blockquote>
<h2 id="function-execution-context">Function Execution Context</h2>
<p>Whenever we invoke a function, a Function Execution Context(FEC) gets created. Function Execution Context is similar to Global     Execution Context and has 2 phases same as Global one - <strong>creation and execution</strong>. The FEC is similar to GEC and has access to a special value called <strong>arguments </strong>but instead of a global object, it creates an <strong>arguments </strong>object.</p>
<blockquote>
<p>Argument Object refers to all the parameters passed to the function.</p>
</blockquote>
<p>These are points that we need to understand here.</p>
<ol>
<li>All the variables and functions in the GEC are still accessible.</li>
<li>When a function calls another function, a new function execution context gets created for the newly called function called and each FEC has its scope of variables.</li>
<li>Once the execution phase of the current FEC is over, it returns the control to the Execution Context where it was called.</li>
</ol>
<h1 id="call-stack">CALL STACK</h1>
<p>To keep a track of all the GEC and FEC, the JS engine uses a data structure called <strong>CALL STACK.</strong>
Stack-based on LIFO (last-in-first-out) principle is used to manage execution contexts and their functioning. Let us discuss the operation performed by call stack maintained by JS engine:</p>
<ol>
<li>At first the stack is <strong>empty</strong>.</li>
<li>When we execute a script, The GEC is created. So it is pushed to the <strong>top </strong>of the stack.</li>
<li>Now whenever a function is called, FEC is created for respective function and it is pushed to the <strong>top </strong>of a stack and starts executing the function. If the function calls another function, then another FEC is created for the called function, pushed to the <strong>top </strong>of the stack, and executed first.</li>
<li>When the execution of the function is complete, the JS engine <strong>pops </strong>the FEC off the stack and executes the FEC at the <strong>top </strong>of the call stack.</li>
<li>When the stack is <strong>empty </strong>the execution stops.</li>
</ol>
<h2 id="stack-overflow">Stack overflow</h2>
<p>The size of the stack is fixed depending on various factors. If the number of Execution Context exceed the stack size, Stack overFlow occur.</p>
<blockquote>
<p>When a recursive function with no exit condition is called.</p>
</blockquote>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623420824130/-CivXfDrO.jpeg" alt="Code.JPG" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623422503640/azALi1PhO.jpeg" alt="Xjkh5YkSm (1).jpg" /></p>
<p>For this example, the <code>var n</code>, <code>var double1</code>, and <code>var double2</code> are given memory, and <code>function double()</code> is copied to the memory as it is. Global Execution Context is pushed to the top of the empty stack</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623420917141/Jf751ufyl.jpeg" alt="memory_creation.jpg" /></p>
<p>After creating memory, the values are assigned to variables in this phase.
Here <code>n=2</code>, that means <code>n</code> is assigned <code>2</code> as value. Also, notice that <code>double1</code> and <code>double2</code> call <code>function double()</code> , so separate execution context are created for each function call.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623421432662/OWq996Kd0.jpeg" alt="execution.jpg" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623422534184/_CCibqunA.jpeg" alt="qpYCndgde.jpg" /></p>
<p><code>double1 =  double(n)</code> is called first so FEC for is created and it is pushed to the top call stack.</p>
<p>Function Execution Context for <code>double(n)</code> is created and the memory creation phase is completed. The argument of the function is also given memory and initialized to special value <code>undefined</code>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623422852063/GFwsernoCJ.jpeg" alt="R374Z-r3F (1).jpg" /></p>
<p>In the execution phase of <code>double(n)</code>, the variables and arguments are initialized to their values. Even the values from the Global Execution Context can be accessed here.
After the encounter of the <code>return</code> keyword, the FEC is ended and gets pop off the stack. <code>double1</code> is returned with value 4 resulting in <code>double1=4</code>. The call stack would look like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623423135709/U5pEm8MpX.png" alt="Xjkh5YkSm.png" /></p>
<p>Now at <code>double2 = double(4)</code> , the <code>double()</code> is again called. Hence a new FEC for this call is created. FEC of this will be pushed at top of the stack. The memory created would occur and in the execution phase And the context and call stack would be like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623423466058/bQBlkzX3M.jpeg" alt="xFo7iAyP2.jpg" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623423617083/TWJfmhTjk.jpeg" alt="Gtmka1SM7.jpg" /></p>
<p>As soon the <code>double2 = double(4)</code> function context returns the computed value, it is popped off the stack. Now there is no more line to execute in the GEC so it is also popped off the stack and the stack is empty. In the end, the memory component would be similar to:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1623423846311/_QTYn14nH.jpeg" alt="dyo9t1M9W.jpg" /></p>
<blockquote>
<p>When Javascript completes the execution, then the Global Execution Context is deleted and Call Stack is empty</p>
</blockquote>
<h1 id="wrap-up">Wrap up</h1>
<p>Execution Context and Call stack are the very important topic for every Javascript developer. Having an idea of how the code works under the hood will sure help in better understanding the code, debugging, and writing cleaner code.
A special thanks to <code>Akshay Saini</code> for explaining these things and encouraging me to help others.
It was an honest effort to help beginners like me to have in-depth knowledge about the Foundations of Javascript.
Thanks for reading! </p>
]]></content:encoded></item></channel></rss>