<?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: Rudresh Shukla</title>
    <description>The latest articles on DEV Community by Rudresh Shukla (@rudreshshukla592).</description>
    <link>https://dev.to/rudreshshukla592</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4038433%2Fc9360414-02a5-4c1d-b5bf-a973ff33bc62.jpg</url>
      <title>DEV Community: Rudresh Shukla</title>
      <link>https://dev.to/rudreshshukla592</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rudreshshukla592"/>
    <language>en</language>
    <item>
      <title>Access Tokens v/s Refresh Tokens Explained: How Authentication Really Works</title>
      <dc:creator>Rudresh Shukla</dc:creator>
      <pubDate>Sat, 05 Sep 2026 11:30:10 +0000</pubDate>
      <link>https://dev.to/rudreshshukla592/access-tokens-vs-refresh-tokens-explained-how-authentication-really-works-4n86</link>
      <guid>https://dev.to/rudreshshukla592/access-tokens-vs-refresh-tokens-explained-how-authentication-really-works-4n86</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsplrqmibpv10e2g7ml0y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsplrqmibpv10e2g7ml0y.jpg" alt=" " width="736" height="414"&gt;&lt;/a&gt;&lt;br&gt;
You log in once, but how does the server know it's still you on every request? Let's understand what happens behind the scenes.&lt;/p&gt;
&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;
&lt;h3&gt;
  
  
  What is authentication?
&lt;/h3&gt;

&lt;p&gt;In simple words, authentication is the process of identifying the user who has made a request. But why do we need it?&lt;/p&gt;

&lt;p&gt;HTTP is stateless, which means each request is independent and carries no memory of previous requests. So, the server/backend can't automatically remember who sent a previous request.&lt;/p&gt;

&lt;p&gt;Don't confuse this with authorization.    &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Authentication&lt;/strong&gt; → Who are you?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authorization&lt;/strong&gt; → What are you allowed to do?&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  What is a Token and Why Do We Need It?
&lt;/h3&gt;

&lt;p&gt;Think of tokens like a temporary ID card that the backend/server gives to the user/client after a successful login. With this ID card, the server can identify and verify the user when they make future requests.&lt;/p&gt;

&lt;p&gt;But why can't we simply send our username and password with every request?&lt;/p&gt;

&lt;p&gt;Because sending private and sensitive credentials again and again is not a good idea and increases the risk of exposing them. Instead, after login, the server can generate a token and send it back to the client. The client can then use that token with future requests, allowing the server to verify the request without asking for the user's password every time.&lt;br&gt;
&lt;/p&gt;

&lt;pre data-lang="mermaid"&gt;&lt;code&gt;flowchart TD
    A[User Logs In] --&amp;gt; B[Backend Verifies Credentials]
    B --&amp;gt; C[Backend Creates Token - JWT]
    C --&amp;gt; D[Frontend Stores Token]
    D --&amp;gt; E[Frontend Sends Token with Future Requests]
    E --&amp;gt; F[Backend Verifies Token]
    F --&amp;gt; G[Backend Responds with Requested Data]&lt;/code&gt;&lt;/pre&gt;



&lt;h2&gt;
  
  
  The Risk Behind Using Tokens
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What If the Token Gets Stolen?
&lt;/h3&gt;

&lt;p&gt;Let's assume a basic scenario: user logs in, server gives token, somehow Attacker gets the token, Attacker sends token to server, Server sees valid token, Request may be accepted.&lt;br&gt;
&lt;/p&gt;

&lt;pre data-lang="mermaid"&gt;&lt;code&gt;flowchart TD
    A[User Logs In] --&amp;gt; B[Server Gives Token]
    B --&amp;gt; C[Attacker Steals Token]
    C --&amp;gt; D[Attacker Sends Token to Server]
    D --&amp;gt; E[Server Sees Valid Token]
    E --&amp;gt; F[Request May Be Accepted]

    style C fill:#ff6b6b,color:#fff
    style D fill:#ff6b6b,color:#fff&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;The server generally cares that the presented credential is valid or not, not whether the person holding it is the original user.&lt;/p&gt;

&lt;p&gt;So if an attacker gets a usable access token, they may be able to access the resources that token permits.&lt;/p&gt;

&lt;p&gt;And that lets us to a point that the longer a token remains valid, the longer an attacker may be able to use it if it gets stolen.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Security vs Convenience Problem
&lt;/h3&gt;

&lt;p&gt;All the Long-lived token are generally very Convenient as created once and use them always but it comes with a cost of security through a scenario we talked above.&lt;/p&gt;

&lt;p&gt;But why can't we simply make every token extremely short-lived?&lt;/p&gt;

&lt;p&gt;Logically Making tokens short-lived reduces the window in which a stolen token can be abused but it could result in user trying to authenticate again and again.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Two-Token Approach
&lt;/h2&gt;

&lt;p&gt;Yes! What if we use two token, but how would that solve the security problem lets see that. But before that lets see what 2 types of token we would be using.&lt;/p&gt;

&lt;h3&gt;
  
  
  Access Token — For Everyday Requests
&lt;/h3&gt;

&lt;p&gt;The Access Token are essential tool used in token-based authentication which allow the user to access normal API requests.&lt;/p&gt;

&lt;p&gt;After a successful login, the backend issues an access token to the client, which is then used as a credential for accessing protected APIs&lt;/p&gt;

&lt;p&gt;They are generally very short-lived, often lasting for minutes rather than days. So if stolen the attacker get's access to private resource for a very limited time resulting in reducing the damage window.&lt;/p&gt;

&lt;h3&gt;
  
  
  Refresh Token — The Key to Staying Logged In
&lt;/h3&gt;

&lt;p&gt;It is the token the client uses to request a new access token from the backend when the current access token expires, most importantly it is not meant to be sent with every API request and only used when access token expires. It is generally a long-lived token.&lt;/p&gt;

&lt;p&gt;This allows the user to stay logged in without entering their password again&lt;/p&gt;

&lt;h3&gt;
  
  
  How They Work Together
&lt;/h3&gt;



&lt;pre data-lang="mermaid"&gt;&lt;code&gt;flowchart TD
    subgraph Login["Login Flow"]
        A[User Sends Email + Password] --&amp;gt; B[Backend Verifies&amp;lt;br/&amp;gt;Password via bcrypt]
        B --&amp;gt; C[Backend Creates&amp;lt;br/&amp;gt;Access Token + Refresh Token]
        C --&amp;gt; D[Store Refresh Token in DB]
        D --&amp;gt; E[Send Both Tokens to Frontend]
    end

    subgraph Request["Normal API Request"]
        F[Frontend Sends Request&amp;lt;br/&amp;gt;+ Access Token] --&amp;gt; G[Backend Verifies Access Token]
        G --&amp;gt;|Valid| H[Request Processed]
        G --&amp;gt;|Invalid / Expired| I[Reject Request]
    end

     subgraph Refresh["Refresh Token Flow - after ~15 min"]
        J[Access Token Expired] --&amp;gt; K[Frontend Sends Refresh Token]
        K --&amp;gt; L[Backend Checks Refresh Token&amp;lt;br/&amp;gt;Against DB]
        L --&amp;gt;|Valid| M[Issue New Access Token&amp;lt;br/&amp;gt;+ New Refresh Token]
        L --&amp;gt;|Invalid| N[User Must Login Again]
        M --&amp;gt; O[Store New Refresh Token in DB]
        O --&amp;gt; P[Send New Access Token&amp;lt;br/&amp;gt;to Frontend]
    end

    E --&amp;gt; F
    I --&amp;gt; J
    P --&amp;gt; F

    NoteA[["🔑 Access Token = Short-lived ~15 min&amp;lt;br/&amp;gt;🔁 Refresh Token = Long-lived days/weeks"]]
    NoteA -.-&amp;gt; C

    style NoteA fill:#fff3cd,stroke:#856404,color:#000&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;In our implementation, the backend stores the refresh token (or its hash) and checks it when a refresh request is made.&lt;/p&gt;

&lt;h2&gt;
  
  
  What If Both Tokens Are Compromised?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Can an Attacker Do With Both Tokens?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Case 1&lt;/strong&gt; - The Access Token is stolen, this could allow the attacker to use the token until it expires, for example, around 15 minutes in our case.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Case 2&lt;/strong&gt; - The Refresh Token is stolen, this could give the attacker the ability to obtain a new access token from the backend.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Case 3&lt;/strong&gt; - Both tokens get stolen, then the attacker can potentially access protected resources and keep obtaining fresh access tokens, depending on how the authentication system is designed and what security measures are implemented.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, if tokens are basically credentials, how do we make sure that stealing them doesn't give an attacker unlimited access?&lt;/p&gt;

&lt;h3&gt;
  
  
  So How Do We Protect Them?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Keep the JWT Secret Safe&lt;/strong&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you're using JWTs, the server signs them with a secret key (or private key), and that same key is what lets it verify later that the token is real and hasn't been tampered with.&lt;/p&gt;

&lt;p&gt;Obviously, this secret needs to stay on the server — it should never end up in your frontend code or anywhere the client can see it.&lt;/p&gt;

&lt;p&gt;But here's the catch: keeping your secret safe doesn't automatically protect you if a token itself gets stolen. If an attacker already has a valid token in hand, they can keep using it like normal until it either expires or you revoke it. The secret being safe just means they can't forge new tokens — it doesn't stop them from using one they already grabbed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Refresh Token Rotation&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Instead of keeping the same refresh token forever, the server can issue a new refresh token whenever the old one is used.&lt;br&gt;
&lt;/p&gt;

&lt;pre data-lang="mermaid"&gt;&lt;code&gt;flowchart TD
    A[Refresh Token A] --&amp;gt; B[Used to Get New Access Token]
    B --&amp;gt; C[New Refresh Token B Issued]
    C --&amp;gt; D[Refresh Token A Becomes Invalid]

    style A fill:#fff3cd,stroke:#856404,color:#000
    style D fill:#ff6b6b,color:#fff&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;This means that if an old refresh token is stolen and someone tries to reuse it after rotation, the server can detect that something suspicious has happened.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping It Up: What I Learned
&lt;/h2&gt;

&lt;p&gt;Before learning about Access and Refresh Tokens, I thought authentication was simply about logging in and getting a token. But after understanding the complete flow, I realized there is a lot more happening behind the scenes.&lt;/p&gt;

&lt;p&gt;The main thing I understood is that both tokens have different jobs. The Access Token is used for normal API requests, while the Refresh Token helps us get a new Access Token when the old one expires.&lt;/p&gt;

&lt;p&gt;What I found most interesting was the security vs convenience part. A short-lived Access Token is safer, but we don't want users to log in again and again. That's where the Refresh Token comes in.&lt;/p&gt;

&lt;p&gt;My biggest takeaway was that authentication isn't just about making login work — it's about making it secure while still keeping the experience smooth for the user.&lt;/p&gt;

&lt;p&gt;Overall, what looked like just two different tokens turned out to be a pretty smart way of balancing security and user experience.&lt;/p&gt;

</description>
      <category>authentication</category>
      <category>backend</category>
      <category>security</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript Under the Hood #1: From Source Code to the Call Stack</title>
      <dc:creator>Rudresh Shukla</dc:creator>
      <pubDate>Mon, 20 Jul 2026 15:43:53 +0000</pubDate>
      <link>https://dev.to/rudreshshukla592/javascript-under-the-hood-1-from-source-code-to-the-call-stack-4n3i</link>
      <guid>https://dev.to/rudreshshukla592/javascript-under-the-hood-1-from-source-code-to-the-call-stack-4n3i</guid>
      <description>&lt;p&gt;Every time you run a JavaScript program, a lot happens behind the scenes. Variables are allocated memory, execution contexts are created, functions are pushed onto the call stack, and the engine starts executing your code. But before we dive into all of that, let's first understand what JavaScript actually is and why it was created.&lt;/p&gt;

&lt;h2&gt;
  
  
  An Introduction to JavaScript
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is JavaScript?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript is a programming language that was originally created in 1995 by Brendan Eich in just 10 days while he was working at Netscape.&lt;/li&gt;
&lt;li&gt;JavaScript is a high-level programming language primarily used to make web pages interactive. Today, it is also used to build servers, mobile applications, desktop software, and much more.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why was JavaScript created?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript was created to make web pages &lt;strong&gt;alive&lt;/strong&gt;. But what does "alive" mean? it means adding interactivity (e.g.,  animations, clickable buttons, popup menus, etc.) to the static web pages.&lt;/li&gt;
&lt;li&gt;Today, JavaScript isn't limited to browsers. With runtimes like Node.js, it can also be used to build backend applications and APIs, which allow you to add more functionality to a website.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Did you know? When JavaScript was created, it initially had another name: “LiveScript”. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Where can JavaScript be used?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;In your browser — every interactive website uses it (Facebook, YouTube, Gmail).&lt;/li&gt;
&lt;li&gt;On servers — through Node.js, you can build backend APIs.&lt;/li&gt;
&lt;li&gt;In mobile apps — using frameworks like React Native.&lt;/li&gt;
&lt;li&gt;In desktop apps — VS Code itself is built using JavaScript (Electron).&lt;/li&gt;
&lt;li&gt;In smart devices, games, robots, and much more.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now that we know what JavaScript is, another question comes to mind: &lt;strong&gt;How does JavaScript execute my code?&lt;/strong&gt; Before answering that, let's first understand &lt;strong&gt;Who executes my code?&lt;/strong&gt;.&lt;br&gt;
The answer is: &lt;strong&gt;The JavaScript Engine&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  The JavaScript Engine
&lt;/h2&gt;

&lt;p&gt;We already know what &lt;strong&gt;JavaScript&lt;/strong&gt; is, but what exactly is this &lt;strong&gt;engine&lt;/strong&gt;?&lt;/p&gt;
&lt;h3&gt;
  
  
  The "Engine"
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A JavaScript engine is a piece of software responsible for executing JavaScript code. Every environment that runs JavaScript, whether it's a browser or Node.js, has its own JavaScript engine. &lt;/li&gt;
&lt;li&gt;But this raises another question: &lt;strong&gt;Why do we even need an engine?&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Why Do We Need a JavaScript Engine?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript cannot execute code on its own. It needs an engine that understands JavaScript syntax and converts it into instructions the computer can execute. Every environment that runs JavaScript has its own engine. For example, Chrome and Node.js use V8, Firefox uses SpiderMonkey, and Safari uses JavaScriptCore.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  How Does the Engine Execute Code?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Modern JavaScript engines process your code in three simple steps:&lt;/li&gt;
&lt;li&gt;Parsing&lt;/li&gt;
&lt;li&gt;Interpreting&lt;/li&gt;
&lt;li&gt;Compiling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Together, these steps are part of a technique known as &lt;strong&gt;Just-In-Time (JIT)&lt;/strong&gt; compilation, which helps JavaScript run quickly while remaining efficient.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌─────────────────────────┐
│  JavaScript Source Code │
└────────────┬────────────┘
             │
             ▼
┌─────────────────────────┐
│   JavaScript Engine     │
└────────────┬────────────┘
             │
             ▼
┌─────────────────────────┐
│   Parses the Code       │
└────────────┬────────────┘
             │
             ▼
┌─────────────────────────┐
│ Processes &amp;amp; Executes It │
└────────────┬────────────┘
             │
             ▼
┌─────────────────────────┐
│ Creates an Execution    │
│ Context                 │
└─────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we know who executes our code and how the engine processes it, let's look at what actually happens the moment JavaScript starts running our program. This is where the concept of an &lt;strong&gt;Execution Context&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Execution Context
&lt;/h2&gt;

&lt;p&gt;Whenever JavaScript executes a program, it always runs inside an execution context.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is an Execution Context (EC)?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;An Execution Context is the &lt;strong&gt;environment&lt;/strong&gt; in which a piece of JavaScript code is evaluated and executed. &lt;/li&gt;
&lt;li&gt;Think of it as a sealed container that holds everything the code needs to run: its variables, functions, and the value of &lt;code&gt;this&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────────────┐
│   JavaScript Code    │
└──────────┬───────────┘
           │
           ▼
┌──────────────────────┐
│ Execution Context    │
├──────────────────────┤
│ • Variables          │
│ • Functions          │
│ • Memory             │
│ • this               │
└──────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why Do We Need an Execution Context (EC)?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Let's look at an example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We know that our code does not run as-is. The JavaScript Engine parses the code and then creates an execution context. The Execution context(EC) is necessary because it allocates memory for variables and functions before the code starts executing.&lt;/li&gt;
&lt;li&gt;So, in this example, the code isn't executed immediately. Before JavaScript starts running each line, the execution context first allocates memory for variables and functions.&lt;/li&gt;
&lt;li&gt;But how does JavaScript allocate memory before executing the code? Let's understand that by looking at the two phases of an execution context.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But before that comes the &lt;strong&gt;types&lt;/strong&gt; of Execution Context.&lt;/p&gt;

&lt;h3&gt;
  
  
  Types of Execution Context
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Global Execution Context (GEC)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Whenever a JavaScript program starts executing, a Global Execution Context (GEC) is created.&lt;/li&gt;
&lt;li&gt;A JavaScript program has only &lt;strong&gt;one&lt;/strong&gt; Global Execution Context (GEC).&lt;/li&gt;
&lt;li&gt;It is also known as the &lt;strong&gt;Base Execution Context&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Function Execution Context (FEC)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When we invoke (call) a function, a Function Execution Context gets created.&lt;/li&gt;
&lt;li&gt;Each function call gets its own private context with its own variables.&lt;/li&gt;
&lt;li&gt;When the function finishes, its FEC is destroyed. &lt;/li&gt;
&lt;li&gt;A Function Execution Context is created during the &lt;strong&gt;Code Execution Phase&lt;/strong&gt; whenever a function is called.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Phases of an Execution Context
&lt;/h3&gt;

&lt;p&gt;This is one of the most important concepts in the whole topic. Every EC is built in two phases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Phase 1 — Memory Creation Phase&lt;/strong&gt;&lt;br&gt;
Before any line runs, JS scans the code and sets up memory:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If there are any &lt;strong&gt;variables&lt;/strong&gt; declared in the code, memory is allocated for the variable. The variables are set to &lt;code&gt;undefined&lt;/code&gt; in this phase.&lt;/li&gt;
&lt;li&gt;If there is a &lt;code&gt;function&lt;/code&gt; declaration in the code, the entire function is stored in memory as-is. &lt;/li&gt;
&lt;li&gt;Also, in this phase, two special values become available:

&lt;ol&gt;
&lt;li&gt;The global object (&lt;code&gt;window&lt;/code&gt; in browsers).&lt;/li&gt;
&lt;li&gt;The global &lt;code&gt;this&lt;/code&gt; value. &lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Phase 2 — Code Execution Phase&lt;/strong&gt;&lt;br&gt;
The code execution starts in this phase.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Here, the real values are assigned to the global variables which were initially set &lt;code&gt;undefined&lt;/code&gt; in Phase 1.&lt;/li&gt;
&lt;li&gt;If there is a function call in the code, then it creates a Function Execution Context in this phase.&lt;/li&gt;
&lt;li&gt;JavaScript waits until that function finishes executing before continuing with the remaining code.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Examples
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Vivek&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now here's what happens:

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Memory Creation Phase&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;username&lt;/code&gt; =&amp;gt; &lt;code&gt;undefined&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;greet&lt;/code&gt; =&amp;gt; full function stored.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; =&amp;gt; set to &lt;code&gt;window&lt;/code&gt; object.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Code Execution Phase&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;username&lt;/code&gt; becomes &lt;strong&gt;Vivek&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Then &lt;code&gt;greet()&lt;/code&gt; is called → a new Function Execution Context is created

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Memory Creation Phase&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;message&lt;/code&gt; → &lt;code&gt;undefined&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Execution Phase&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;message&lt;/code&gt; becomes &lt;strong&gt;Hello&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hello&lt;/strong&gt; gets printed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;The FEC gets destroyed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Vivek&lt;/strong&gt; gets printed.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Final Output: 

&lt;ul&gt;
&lt;li&gt;Hello&lt;/li&gt;
&lt;li&gt;Vivek
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────────────────────────┐
│ Global Execution Context (GEC)   │
├──────────────────────────────────┤
│ • Memory Creation Phase          │
│ • Code Execution Phase           │
└───────────────┬──────────────────┘
                │
                │ greet()
                ▼
┌──────────────────────────────────┐
│ Function Execution Context (FEC) │
├──────────────────────────────────┤
│ • Memory Creation Phase          │
│ • Code Execution Phase           │
└───────────────┬──────────────────┘
                │
                │ Function ends
                ▼
        Return to the GEC
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Since memory is created before code executes, this leads us to one of JavaScript's most misunderstood concepts: Hoisting.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hoisting
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is Hoisting?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Hoisting is JS's behaviour of &lt;strong&gt;appearing&lt;/strong&gt; to move declarations of functions and variables to the top of their &lt;strong&gt;scope&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;Important: nothing physically moves — this is just the &lt;strong&gt;Creation Phase&lt;/strong&gt; allocating memory before the code runs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why Does Hoisting Happen?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;In the Execution Context, memory is created before code executes. This leads to Hoisting.&lt;/li&gt;
&lt;li&gt;Let's take an example.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Vivek&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The output of above code is &lt;code&gt;undefined&lt;/code&gt;. But why? Suppose we use similar code in some other programming language. In that case, we may get an error saying the variable name is not declared, as we are trying to access it well before that.&lt;/li&gt;
&lt;li&gt;Let's visualize the &lt;strong&gt;Global Execution Context&lt;/strong&gt; for the example which will give us more clarity:

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Memory Creation Phase&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt; =&amp;gt; &lt;code&gt;undefined&lt;/code&gt; &lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Code Execution Phase&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;console.log(name)&lt;/code&gt; =&amp;gt; prints &lt;code&gt;undefined&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt; =&amp;gt; &lt;strong&gt;Vivek&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;See what happened here in the &lt;strong&gt;Code Execution Phase&lt;/strong&gt; the console.log() prints the value of &lt;code&gt;name&lt;/code&gt; before it can even be assigned to its real value.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Hoisting with var
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;var&lt;/code&gt; declarations are hoisted and set to &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;So you can access them before the line they're written on — you just get &lt;code&gt;undefined&lt;/code&gt;, not an error.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// undefined  (not an error!)&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Hoisting with let &amp;amp; const
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are hoisted too — but they are not initialized.&lt;/li&gt;
&lt;li&gt;From the start of their scope until the line where they are declared, they sit in the &lt;strong&gt;Temporal Dead Zone (TDZ)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;So, accessing them before their declaration throws a &lt;code&gt;ReferenceError&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ❌ ReferenceError: Cannot access 'a' before initialization&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// OR&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ❌ ReferenceError: Cannot access 'b' before initialization&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Temporal Dead Zone (TDZ)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It is a specific region of a block scope where a variable exists but is completely inaccessible before its actual declaration line.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Attempting to access, read, or write to a variable while it is trapped in the TDZ will immediately throw a &lt;code&gt;ReferenceError&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For this example, the &lt;strong&gt;Global Execution Context (GEC)&lt;/strong&gt; is visualized below, showing the &lt;strong&gt;Temporal Dead Zone (TDZ)&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ❌ ReferenceError: Cannot access 'b' before initialization&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌────────────────────────────────────────────────────────────────────┐
│                   Global Execution Context (GEC)                   │
├────────────────────────────────────────────────────────────────────┤
│                                                                    |
│   Memory Creation Phase                                            │
│   ────────────────────────────────────────                         │
│   b  →  &amp;lt;uninitialized&amp;gt;   ← NOT undefined                          │
│                                                                    │
├────────────────────────────────────────────────────────────────────┤
│                                                                    │
│   Code Execution Phase                                             │
│   ────────────────────────────────────────                         │
│   console.log(b);                                                  │
│   ▲                                                                │
│   │                                                                │
│   │   [ERROR] Temporal Dead Zone (TDZ)                             │
│   │   Cannot access 'b' before initialization                      │
│   │                                                                │
│   let b = 10;   ← TDZ ends here                                    │
│                                                                    │
│   b = 10                                                           │
│                                                                    │
└────────────────────────────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;💡 Remember&lt;/p&gt;

&lt;p&gt;Hoisting is not JavaScript moving your code.&lt;br&gt;
It's the result of the Memory Creation Phase, where memory is allocated before the Code Execution Phase begins.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Call Stack
&lt;/h2&gt;

&lt;p&gt;JavaScript can execute only one piece of code at a time. So whenever a function is called, JavaScript needs a way to remember where it currently is and which function should execute next. This is exactly what the &lt;strong&gt;Call Stack&lt;/strong&gt; does.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Call Stack?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The Call Stack is a data structure that tracks the execution of functions and keeps track of which function is currently running.&lt;/li&gt;
&lt;li&gt;It is a mechanism used by the JavaScript engine.&lt;/li&gt;
&lt;li&gt;It works on LIFO — Last In, First Out principle.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why Do We Need a Call Stack?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Without the call stack, JavaScript would quickly break down for the following critical reasons:&lt;/li&gt;
&lt;li&gt;Tracking where JavaScript currently is.&lt;/li&gt;
&lt;li&gt;Executing functions in the correct order.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How Does the Call Stack Work?
&lt;/h3&gt;

&lt;p&gt;The Call Stack operates on a Last-In, First-Out (LIFO) principle. Think of it like a stack of books—the last book you put on top is the first one you must take off.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Push&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When you call (invoke) a function =&amp;gt; it gets added (pushed) to the top of the stack.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Pop&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When a function returns / finishes =&amp;gt; JavaScript removes (pops) it off the top of the stack.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Call Stack Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;one&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;One&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;two&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;one&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Two&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;three&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;two&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Three&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;three&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step-by-step stack movement:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Step&lt;/th&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;Stack (top → bottom)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;three()&lt;/code&gt; called&lt;/td&gt;
&lt;td&gt;three → GEC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;two()&lt;/code&gt; called inside three&lt;/td&gt;
&lt;td&gt;two → three → GEC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;one()&lt;/code&gt; called inside two&lt;/td&gt;
&lt;td&gt;one → two → three → GEC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;one()&lt;/code&gt; finishes → prints&lt;/td&gt;
&lt;td&gt;(pops one) two → three → GEC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;two()&lt;/code&gt; finishes → prints&lt;/td&gt;
&lt;td&gt;(pops two) three → GEC&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;three()&lt;/code&gt; finishes → prints&lt;/td&gt;
&lt;td&gt;(pops three) GEC&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Visualization&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Initial Call Stack&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────┐
│     GEC      │
└──────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. &lt;code&gt;three()&lt;/code&gt; called&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────┐
│    three()   │
├──────────────┤
│     GEC      │
└──────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. &lt;code&gt;two()&lt;/code&gt; called&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────┐
│    two()     │
├──────────────┤
│    three()   │
├──────────────┤
│     GEC      │
└──────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. &lt;code&gt;one()&lt;/code&gt; called&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────┐
│    one()     │
├──────────────┤
│    two()     │
├──────────────┤
│    three()   │
├──────────────┤
│     GEC      │
└──────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. &lt;code&gt;one()&lt;/code&gt; finishes&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────┐
│    two()     │
├──────────────┤
│    three()   │
├──────────────┤
│     GEC      │
└──────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;6. &lt;code&gt;two()&lt;/code&gt; finishes&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────┐
│    three()   │
├──────────────┤
│     GEC      │
└──────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;7. &lt;code&gt;three()&lt;/code&gt; finishes&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────┐
│     GEC      │
└──────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Output order:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;One&lt;/li&gt;
&lt;li&gt;Two&lt;/li&gt;
&lt;li&gt;Three&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Stack Overflow
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Since memory is finite, the Call Stack has a size limit.&lt;/li&gt;
&lt;li&gt;If functions keep getting pushed without ever popping (usually infinite recursion), the stack runs out of space and the engine throws:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// calls itself forever, never returns&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// ❌ RangeError: Maximum call stack size exceeded&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Every recursive function must have a base case that stops the recursion, otherwise → stack overflow.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 Remember&lt;/p&gt;

&lt;p&gt;The Call Stack doesn't run your code — it just tracks &lt;em&gt;where&lt;/em&gt; the engine currently is. LIFO in, LIFO out.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Putting It All Together
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Now rather than introducing any new concept Let's solve an example for better clarity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;1. Code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Vivek&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Execution Flow&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JavaScript Source Code
          │
          ▼
┌──────────────────────┐
│ JavaScript Engine    │
└──────────┬───────────┘
           │
           ▼
┌──────────────────────┐
│ Global Execution     │
│ Context Created      │
└──────────┬───────────┘
           │
           ▼
┌──────────────────────┐
│ Memory Creation      │
│ Phase                │
└──────────┬───────────┘
           │
           ▼
┌──────────────────────┐
│ Code Execution       │
│ Phase                │
└──────────┬───────────┘
           │
           ▼
┌──────────────────────┐
│ Function Call        │
│ greet()              │
└──────────┬───────────┘
           │
           ▼
┌──────────────────────┐
│ Function Execution   │
│ Context              │
└──────────┬───────────┘
           │
           ▼
┌──────────────────────┐
│ Console Output       │
│ Hello                │
│ Vivek                │
└──────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Global Execution Context&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌────────────────────────────────────────────┐
│ Global Execution Context                   │
├────────────────────────────────────────────┤
│ Memory Creation Phase                      │
├────────────────────────────────────────────┤
│ username → undefined                       │
│ greet    → Entire Function Stored          │
│ this     → window                          │
├────────────────────────────────────────────┤
│ Code Execution Phase                       │
├────────────────────────────────────────────┤
│ username = "Vivek"                         │
│ greet() ← Function Called                  │
│ console.log(username)                      │
└────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Call Stack&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;1. Initial Call Stack&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────┐
│     GEC      │
└──────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. &lt;code&gt;greet()&lt;/code&gt; called&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────┐
│   greet()    │
├──────────────┤
│     GEC      │
└──────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. &lt;code&gt;greet()&lt;/code&gt; finishes&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────┐
│     GEC      │
└──────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5. Final Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;Console

Hello
Vivek
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Final Summary (End the article with this)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In this article, we followed the complete journey of a JavaScript program—from understanding what JavaScript is, to learning how the JavaScript Engine processes code, how an Execution Context is created, why Hoisting occurs, and finally how the Call Stack manages function execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;These concepts form the foundation of JavaScript. Once they become clear, understanding advanced topics like Closures, the Event Loop, Promises, and Async/Await becomes much easier.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the next part of this series, we'll dive deeper into how JavaScript handles asynchronous operations and explore the Event Loop in detail.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;If you found this article helpful, consider leaving a ❤️ and sharing your thoughts in the comments.&lt;/p&gt;

&lt;p&gt;The next article in this series will cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript Event Loop&lt;/li&gt;
&lt;li&gt;Web APIs&lt;/li&gt;
&lt;li&gt;Callback Queue&lt;/li&gt;
&lt;li&gt;Microtasks vs Macrotasks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See you in Part 2!&lt;/p&gt;

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