<?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[Execution-Context-in-JS]]></title><description><![CDATA[Execution-Context-in-JS]]></description><link>https://explore-execution-context-in-js.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Mon, 12 Oct 2026 04:53:43 GMT</lastBuildDate><atom:link href="https://explore-execution-context-in-js.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[  🧠 How JavaScript Program Executes Behind the Scenes]]></title><description><![CDATA[Before understanding Call Stack, Memory, and Execution Context, we need to understand one fundamental thing:
👉 Every program needs memory to run.
When a JavaScript program runs:

It needs memory to s]]></description><link>https://explore-execution-context-in-js.hashnode.dev/execution-context-in-js</link><guid isPermaLink="true">https://explore-execution-context-in-js.hashnode.dev/execution-context-in-js</guid><category><![CDATA[ChaiCode]]></category><category><![CDATA[Execution Context]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Babita Kumari]]></dc:creator><pubDate>Mon, 23 Feb 2026 18:36:29 GMT</pubDate><content:encoded><![CDATA[<p>Before understanding Call Stack, Memory, and Execution Context, we need to understand one fundamental thing:</p>
<p>👉 <strong>Every program needs memory to run.</strong></p>
<p>When a JavaScript program runs:</p>
<ul>
<li><p>It needs memory to store variables and functions.</p>
</li>
<li><p>It needs a place to execute code line by line.</p>
</li>
<li><p>It needs a mechanism to manage function calls.</p>
</li>
</ul>
<p>JavaScript does this using something called:</p>
<blockquote>
<p>Execution Context.</p>
</blockquote>
<h2>📌 What is Execution Context?</h2>
<p>An <strong>Execution Context</strong> is the environment in which JavaScript code is evaluated and executed.</p>
<p>Whenever JavaScript runs code, it creates an execution context.</p>
<p>There are mainly two types:</p>
<ul>
<li><p><strong>Global Execution Context (GEC)</strong></p>
</li>
<li><p><strong>Function Execution Context (FEC)</strong></p>
</li>
</ul>
<h2>🌍 1. Global Execution Context (GEC)</h2>
<h3>What is it?</h3>
<p>The <strong>Global Execution Context</strong> is created:</p>
<ul>
<li><p>When the JavaScript file first loads.</p>
</li>
<li><p>Before any code starts executing.</p>
</li>
</ul>
<p>It is the <strong>default context</strong> where your entire program begins.</p>
<p>There is only <strong>one Global Execution Context per program.</strong></p>
<h2>🔁 2. Function Execution Context (FEC)</h2>
<p>Now let’s talk about what happens when a function is called.</p>
<p>Whenever a function is invoked:</p>
<p>👉 A <strong>new Execution Context</strong> is created for that function.</p>
<p>This is called:</p>
<blockquote>
<p><strong>Function Execution Context (FEC)</strong></p>
</blockquote>
<p>Each time a function runs, it gets its own separate execution environment.</p>
<h2>What Does Each Execution Context Contain?</h2>
<img src="https://cloudmate-test.s3.us-east-1.amazonaws.com/uploads/covers/695252f9d6df54dba55a5b5a/2d8222dc-ef90-4d71-b48b-6f7d0a8fc47b.jpg" alt="" style="display:block;margin:0 auto" />

<p>Each Execution Context has:</p>
<ul>
<li><p><strong>Lexical Environment</strong>:<br />It contains the <code>variable environment</code> and a <code>reference</code> to the outer environment (this forms the scope chain).</p>
</li>
<li><p><strong>this</strong>:<br />The value of <code>this</code> is determined depending on how the function is called.</p>
</li>
<li><p><strong>Some internal metaData</strong>:<br />Internal engine-level information needed to manage execution.</p>
</li>
</ul>
<h2>⚠️ Important Note</h2>
<p>Always keep in mind:</p>
<blockquote>
<p><strong>Heap is not a part of the Execution Context.</strong></p>
</blockquote>
<p>Execution Context contains a <strong>Variable Environment</strong>, and that variable environment may hold <strong>references to non-primitive data stored in the Heap.</strong></p>
<p>In simple terms:</p>
<ul>
<li><p>Execution Context lives in the <strong>Call Stack</strong>.</p>
</li>
<li><p>Objects and complex data live in the <strong>Heap</strong>.</p>
</li>
<li><p>Variables inside the Execution Context may point to memory in the Heap.</p>
</li>
</ul>
<h2>Understanding the Diagram (Call Stack + Heap + Execution Context)</h2>
<img src="https://cloudmate-test.s3.us-east-1.amazonaws.com/uploads/covers/695252f9d6df54dba55a5b5a/f03fb2e8-23a6-4231-b02b-25543e08cb6a.jpg" alt="" style="display:block;margin:0 auto" />

<p>The above diagram shows how JavaScript executes the following code:</p>
<pre><code class="language-plaintext">var a = 5;

function f1(){
  let b = 6;
  console.log(a);
  console.log(b);
}

f1();
</code></pre>
<hr />
<h2>1️⃣ Global Execution Context (Bottom of Call Stack)</h2>
<p>When the program starts:</p>
<ul>
<li><p>The <strong>Global Execution Context (GEC)</strong> is created.</p>
</li>
<li><p>It is pushed into the <strong>Call Stack</strong>.</p>
</li>
</ul>
<p>Inside Global Execution Context:</p>
<ul>
<li><p><code>a → 5</code></p>
</li>
<li><p><code>f1 → reference to function (stored in Heap)</code></p>
</li>
<li><p>Outer reference → <code>null</code></p>
</li>
</ul>
<p>Important:</p>
<p>👉 The actual function object is stored in the <strong>Heap</strong>.  </p>
<p>👉 The Global Execution Context only stores a <strong>reference</strong> to it.</p>
<h2>2️⃣ Heap (Right Side)</h2>
<p>The Heap stores:</p>
<ul>
<li><p>The actual <code>f1</code> function code.</p>
</li>
<li><p>Any non-primitive data (objects, arrays, functions).</p>
</li>
</ul>
<p>So:</p>
<ul>
<li><p><code>f1</code> in Global EC → holds a reference</p>
</li>
<li><p>Real function definition → lives in Heap</p>
</li>
</ul>
<p>Execution Context does NOT store function body directly — it stores reference.</p>
<h2>3️⃣ Function Call → New Execution Context</h2>
<p>When <code>f1()</code> runs:</p>
<ul>
<li><p>A <strong>Function Execution Context (FEC)</strong> is created.</p>
</li>
<li><p>It is pushed on top of the Call Stack.</p>
</li>
</ul>
<p>Now Call Stack looks like:</p>
<pre><code class="language-plaintext">Function Execution Context (f1)
Global Execution Context
</code></pre>
<h2>4️⃣ Inside Function Execution Context</h2>
<p>Inside FEC:</p>
<ul>
<li><p><code>b → 6</code></p>
</li>
<li><p>Outer Reference → points to Global Execution Context</p>
</li>
<li><p><code>this</code> is set (depends on call)</p>
</li>
</ul>
<p>Now when:</p>
<pre><code class="language-plaintext">console.log(a);
</code></pre>
<p>JavaScript searches:</p>
<ol>
<li><p>Inside FEC → not found</p>
</li>
<li><p>Goes to Outer Reference (Global EC) → finds <code>a = 5</code></p>
</li>
</ol>
<p>This is the <strong>Scope Chain</strong>.</p>
<p>Then:</p>
<pre><code class="language-plaintext">console.log(b);
</code></pre>
<p>It finds <code>b</code> inside FEC.</p>
<h2>5️⃣ Function Finishes</h2>
<p>After execution:</p>
<ul>
<li><p>Function Execution Context is popped from the Call Stack.</p>
</li>
<li><p>Only Global Execution Context remains.</p>
</li>
</ul>
<p>Stack becomes:</p>
<pre><code class="language-plaintext">Global Execution Context
</code></pre>
<hr />
<h2>What This Diagram Shows Clearly</h2>
<ul>
<li><p>Call Stack manages Execution Contexts.</p>
</li>
<li><p>Each function call creates a new Execution Context.</p>
</li>
<li><p>Execution Context contains variables and references.</p>
</li>
<li><p>Heap stores actual objects and function definitions.</p>
</li>
<li><p>Scope chain works using Outer Environment Reference.</p>
</li>
</ul>
<h1>✨ Conclusion</h1>
<p>Now you’ve seen how JavaScript doesn’t simply execute code line by line.<br />It creates Execution Contexts, manages memory using the Call Stack, stores complex data in the Heap, and uses the scope chain to resolve variables.</p>
<p>Understanding Global Execution Context, Function Execution Context, and how they interact with the Call Stack and Heap gives you a much deeper clarity of how JavaScript truly works behind the scenes.</p>
<p><strong>I hope this blog helps you understand how JavaScript programs execute behind the scenes.</strong></p>
]]></content:encoded></item></channel></rss>