<?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[Deep Javascript Concepts]]></title><description><![CDATA[Deep Javascript Concepts]]></description><link>https://deep-javascript-concepts.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 30 Sep 2026 13:33:04 GMT</lastBuildDate><atom:link href="https://deep-javascript-concepts.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[02 How JavaScript code is Executed & Call Stack]]></title><description><![CDATA[🚀 JavaScript Code Execution Process
JavaScript is a single-threaded, synchronous, non-blocking language that uses an event-driven model with a call stack, heap, Web APIs, task queue, and event loop.


🔁 Step-by-Step: How JS Executes Code

Parsing &...]]></description><link>https://deep-javascript-concepts.hashnode.dev/02-how-javascript-code-is-executed-and-call-stack</link><guid isPermaLink="true">https://deep-javascript-concepts.hashnode.dev/02-how-javascript-code-is-executed-and-call-stack</guid><category><![CDATA[Execution Context]]></category><category><![CDATA[callstack]]></category><dc:creator><![CDATA[Kamlesh Choudhary]]></dc:creator><pubDate>Fri, 15 Aug 2025 10:12:35 GMT</pubDate><content:encoded><![CDATA[<hr />
<h2 id="heading-javascript-code-execution-process">🚀 JavaScript Code Execution Process</h2>
<p>JavaScript is a <strong>single-threaded, synchronous, non-blocking</strong> language that uses an <strong>event-driven model</strong> with a <strong>call stack</strong>, <strong>heap</strong>, <strong>Web APIs</strong>, <strong>task queue</strong>, and <strong>event loop</strong>.</p>
<hr />
<p><img alt="final_execution_context.jpg" /></p>
<h3 id="heading-step-by-step-how-js-executes-code">🔁 Step-by-Step: How JS Executes Code</h3>
<ol>
<li><p><strong>Parsing &amp; Compilation Phase</strong></p>
<ul>
<li><p>Code is parsed by the JS engine (e.g., V8 in Chrome).</p>
</li>
<li><p>Function and variable declarations are <strong>hoisted</strong>.</p>
</li>
<li><p>Scope and memory are set up.</p>
</li>
</ul>
</li>
<li><p><strong>Execution Phase</strong></p>
<ul>
<li><p>Code runs <strong>top to bottom</strong>, line by line.</p>
</li>
<li><p>Functions are pushed/popped from the <strong>call stack</strong> when invoked.</p>
</li>
<li><p>Variables are resolved using the <strong>scope chain</strong>.</p>
</li>
</ul>
</li>
</ol>
<hr />
<h2 id="heading-components-of-javascript-runtime">📚 Components of JavaScript Runtime</h2>
<h3 id="heading-1-call-stack">1. <strong>Call Stack</strong></h3>
<p>A <strong>LIFO (Last In, First Out)</strong> structure that tracks function calls.</p>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">a</span>(<span class="hljs-params"></span>) </span>{
  b();
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">b</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello"</span>);
}

a();
</code></pre>
<h3 id="heading-call-stack-flow">🔍 Call Stack Flow:</h3>
<pre><code class="lang-plaintext">Global()
→ a()
   → b()
      → console.log()
</code></pre>
<p><strong>Stack Push/Pop:</strong></p>
<ul>
<li><p><code>Global()</code> → pushed first</p>
</li>
<li><p><code>a()</code> → pushed</p>
</li>
<li><p><code>b()</code> → pushed</p>
</li>
<li><p><code>console.log()</code> → pushed → popped</p>
</li>
<li><p><code>b()</code> → popped</p>
</li>
<li><p><code>a()</code> → popped</p>
</li>
<li><p><code>Global()</code> → popped</p>
</li>
</ul>
<hr />
<h3 id="heading-2-memory-heap">2. <strong>Memory Heap</strong></h3>
<ul>
<li><p>Where all objects, arrays, and functions are stored.</p>
</li>
<li><p>Unstructured region for <strong>dynamic memory allocation</strong>.</p>
</li>
</ul>
<hr />
<h3 id="heading-3-web-apis-browser-environment">3. <strong>Web APIs</strong> (Browser Environment)</h3>
<p>JS has access to browser-provided APIs like:</p>
<ul>
<li><p><code>setTimeout</code></p>
</li>
<li><p>DOM Events</p>
</li>
<li><p>Fetch/XHR</p>
<p>  These run <strong>outside</strong> the JS engine.</p>
</li>
</ul>
<hr />
<h3 id="heading-4-callback-task-queue">4. <strong>Callback / Task Queue</strong></h3>
<p>After a Web API (like <code>setTimeout</code>) finishes, it sends the callback to the <strong>task queue</strong>, waiting for the call stack to be empty.</p>
<hr />
<h3 id="heading-5-event-loop">5. <strong>Event Loop</strong></h3>
<ul>
<li><p>Continuously checks if the call stack is empty.</p>
</li>
<li><p>If it is, it <strong>moves the first task</strong> from the queue to the stack.</p>
</li>
</ul>
<hr />
<h2 id="heading-example-with-settimeout">⏱️ Example with <code>setTimeout</code></h2>
<pre><code class="lang-jsx"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">"1"</span>);

<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"2"</span>);
}, <span class="hljs-number">0</span>);

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"3"</span>);
</code></pre>
<h3 id="heading-output">Output:</h3>
<pre><code class="lang-plaintext">1
3
2
</code></pre>
<h3 id="heading-what-happens">🔄 What Happens:</h3>
<ol>
<li><p><code>console.log("1")</code> → Call Stack → logs 1</p>
</li>
<li><p><code>setTimeout</code> → Web API → after 0ms → pushes callback to queue</p>
</li>
<li><p><code>console.log("3")</code> → Call Stack → logs 3</p>
</li>
<li><p>Call Stack is now empty → Event Loop moves callback → Call Stack → logs 2</p>
</li>
</ol>
<hr />
<h2 id="heading-key-concepts">🧠 Key Concepts</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Concept</td><td>Description</td></tr>
</thead>
<tbody>
<tr>
<td>Call Stack</td><td>Tracks function calls</td></tr>
<tr>
<td>Memory Heap</td><td>Stores variables and objects</td></tr>
<tr>
<td>Web APIs</td><td>Browser features (timers, DOM, etc.)</td></tr>
<tr>
<td>Callback Queue</td><td>Stores async tasks waiting to run</td></tr>
<tr>
<td>Event Loop</td><td>Moves tasks from queue to stack when ready</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-visual-summary">🎯 Visual Summary</h2>
<pre><code class="lang-plaintext">  ┌─────────────┐        ┌────────────┐
  │ Call Stack  │        │ Task Queue │
  └────▲───┬────┘        └────┬───▲────┘
       │   │                   │   │
       │   └─ console.log()    │   └─ (After async complete)
       │                       │
       │   ┌─────────────┐     │
       └──▶│ Web APIs     │────┘
           └─────────────┘
</code></pre>
<hr />
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1755252590362/a2f11b32-d837-4646-972c-5ab544f6070f.jpeg" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1755252657854/0e7db235-3a16-4080-b5c2-8f27499c2024.png" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[01  How JavaScript Works & Execution Context]]></title><description><![CDATA[Let's dive into how JavaScript works, starting from the core engine mechanics to the Execution Context concept. This is Lesson 01: How JavaScript Works & Execution Context – a foundational topic for mastering JS internals.

🔥 01. How JavaScript Work...]]></description><link>https://deep-javascript-concepts.hashnode.dev/01-how-javascript-works-and-execution-context</link><guid isPermaLink="true">https://deep-javascript-concepts.hashnode.dev/01-how-javascript-works-and-execution-context</guid><category><![CDATA[Execution Context]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Kamlesh Choudhary]]></dc:creator><pubDate>Fri, 15 Aug 2025 09:54:42 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/qaedPly-Uro/upload/5033aa2321f8e496bc8716707a81eb5c.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Let's dive into <strong>how JavaScript works</strong>, starting from the <strong>core engine mechanics</strong> to the <strong>Execution Context</strong> concept. This is <strong>Lesson 01: How JavaScript Works &amp; Execution Context</strong> – a foundational topic for mastering JS internals.</p>
<hr />
<h2 id="heading-01-how-javascript-works-overview">🔥 01. How JavaScript Works – Overview</h2>
<p>JavaScript is:</p>
<ul>
<li><p><strong>Single-threaded</strong> → one task at a time</p>
</li>
<li><p><strong>Synchronous by default</strong> (but supports async with Web APIs)</p>
</li>
<li><p><strong>Interpreted/Just-in-time compiled</strong> (V8 engine etc.)</p>
</li>
</ul>
<p>JavaScript runs inside the <strong>JavaScript Engine</strong> (e.g., V8 in Chrome, SpiderMonkey in Firefox). It consists of:</p>
<ul>
<li><p><strong>Call Stack</strong></p>
</li>
<li><p><strong>Memory Heap</strong></p>
</li>
<li><p><strong>Execution Context</strong></p>
</li>
<li><p><strong>Event Loop</strong></p>
</li>
</ul>
<hr />
<h2 id="heading-javascript-engine-workflow">⚙️ JavaScript Engine Workflow</h2>
<ol>
<li><p><strong>Parsing Phase</strong> (Compile Time)</p>
<ul>
<li><p>Code is parsed into <strong>tokens</strong></p>
</li>
<li><p>Converted into <strong>Abstract Syntax Tree (AST)</strong></p>
</li>
<li><p>Variables and function declarations are <strong>hoisted</strong></p>
</li>
</ul>
</li>
<li><p><strong>Execution Phase</strong></p>
<ul>
<li><p>JS runs top-to-bottom</p>
</li>
<li><p>Creates Execution Contexts for each scope</p>
</li>
<li><p>Handles function calls using the <strong>Call Stack</strong></p>
</li>
</ul>
</li>
</ol>
<hr />
<h2 id="heading-execution-context-ec-core-concept">🧠 Execution Context (EC) – Core Concept</h2>
<p>An <strong>Execution Context</strong> is an environment where JavaScript code is evaluated and executed.</p>
<h3 id="heading-there-are-3-main-types">There are 3 main types:</h3>
<ol>
<li><p><strong>Global Execution Context (GEC)</strong></p>
</li>
<li><p><strong>Function Execution Context (FEC)</strong></p>
</li>
<li><p><strong>Eval Execution Context</strong> (rarely used)❌</p>
</li>
</ol>
<hr />
<h3 id="heading-global-execution-context">🔹 Global Execution Context</h3>
<p>Created when the JS file is loaded:</p>
<ul>
<li><p>Creates a global object (<code>window</code> in browsers, <code>global</code> in Node.js)</p>
</li>
<li><p>Binds <code>this</code> to global object</p>
</li>
<li><p>Hoists global variables and functions</p>
</li>
</ul>
<h3 id="heading-example">Example:</h3>
<pre><code class="lang-jsx"><span class="hljs-keyword">var</span> a = <span class="hljs-number">10</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">greet</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Hello"</span>);
}
</code></pre>
<p>→ GEC is created, where:</p>
<ul>
<li><p><code>a</code> is added to memory</p>
</li>
<li><p><code>greet</code> function is hoisted</p>
</li>
</ul>
<hr />
<h3 id="heading-function-execution-context">🔹 Function Execution Context</h3>
<p>Created every time a function is <strong>invoked</strong>:</p>
<ul>
<li><p>Has its own <strong>Variable Environment (VE)</strong></p>
</li>
<li><p>Arguments, local variables, inner functions live here</p>
</li>
<li><p>Forms part of the <strong>Call Stack</strong></p>
</li>
</ul>
<h3 id="heading-example-1">Example:</h3>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span>(<span class="hljs-params">x, y</span>) </span>{
  <span class="hljs-keyword">var</span> total = x + y;
  <span class="hljs-keyword">return</span> total;
}

add(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>);
</code></pre>
<p>When <code>add(3, 4)</code> is called:</p>
<ol>
<li><p>New <strong>FEC</strong> is created</p>
</li>
<li><p>Parameters <code>x=3</code>, <code>y=4</code>, local <code>total=7</code></p>
</li>
<li><p>FEC is popped off after return</p>
</li>
</ol>
<hr />
<h2 id="heading-whats-inside-an-execution-context">📦 What’s Inside an Execution Context?</h2>
<p>Every EC contains:</p>
<ol>
<li><p><strong>Variable Environment (VE)</strong></p>
<ul>
<li><p><code>var</code>, <code>function</code> declarations</p>
</li>
<li><p><code>arguments</code> object</p>
</li>
</ul>
</li>
<li><p><strong>Lexical Environment</strong></p>
<ul>
<li><p>Tracks where variables/functions were defined</p>
</li>
<li><p>Helps with <strong>scope chain</strong></p>
</li>
</ul>
</li>
<li><p><code>this</code> Binding</p>
<ul>
<li>Depends on <strong>how a function is called</strong></li>
</ul>
</li>
</ol>
<hr />
<h2 id="heading-execution-stack-call-stack">🔁 Execution Stack (Call Stack)</h2>
<p>The <strong>Call Stack</strong> manages Execution Contexts in a <strong>LIFO</strong> manner:</p>
<pre><code class="lang-jsx"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">one</span>(<span class="hljs-params"></span>) </span>{
  two();
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">two</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"2"</span>);
}
one();
</code></pre>
<h3 id="heading-call-stack-flow">Call Stack Flow:</h3>
<pre><code class="lang-plaintext">Global EC
→ one() EC
  → two() EC
    → console.log() EC
</code></pre>
<p>Each function creates a new EC which is pushed onto the stack, then popped off when done.</p>
<hr />
<h2 id="heading-summary">🧠 Summary</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Term</td><td>Meaning</td></tr>
</thead>
<tbody>
<tr>
<td>Execution Context</td><td>The environment where code is run</td></tr>
<tr>
<td>Global EC</td><td>Created once when script loads</td></tr>
<tr>
<td>Function EC</td><td>Created every time a function runs</td></tr>
<tr>
<td>Call Stack</td><td>Tracks active function calls</td></tr>
<tr>
<td>Variable Environment</td><td>Stores function variables &amp; arguments</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-visual-mind-map">🔍 Visual Mind Map</h2>
<pre><code class="lang-plaintext">JavaScript Engine
├─ Memory Heap
├─ Call Stack
│  ├─ GEC
│  ├─ FEC
│  └─ Eval EC (rare)
└─ Event Loop (later topic)
</code></pre>
<blockquote>
<ul>
<li><p>The article explores JavaScript's core mechanics, focusing on the JavaScript Engine and Execution Contexts.</p>
</li>
<li><p>It explains how JavaScript is single-threaded and synchronous, detailing the Parsing and Execution phases.</p>
</li>
<li><p>Key components such as the Global Execution Context, Function Execution Context, and Call Stack are explained.</p>
</li>
<li><p>It highlights how these components manage code execution and variable storage.</p>
</li>
<li><p>The structure of Execution Contexts is discussed, including the Variable Environment, Lexical Environment, and <code>this</code> binding.</p>
</li>
<li><p>Provides a foundational understanding of JavaScript's internal workings.</p>
</li>
</ul>
</blockquote>
<hr />
]]></content:encoded></item></channel></rss>