<?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: Adel Stiti</title>
    <description>The latest articles on DEV Community by Adel Stiti (@adelstiti).</description>
    <link>https://dev.to/adelstiti</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3593010%2F8d6bb410-69da-48e7-be65-317835b5d787.png</url>
      <title>DEV Community: Adel Stiti</title>
      <link>https://dev.to/adelstiti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adelstiti"/>
    <language>en</language>
    <item>
      <title>How JavaScript Works: The Runtime Environment</title>
      <dc:creator>Adel Stiti</dc:creator>
      <pubDate>Tue, 04 Nov 2025 06:50:48 +0000</pubDate>
      <link>https://dev.to/adelstiti/how-javascript-works-the-runtime-environment-3e52</link>
      <guid>https://dev.to/adelstiti/how-javascript-works-the-runtime-environment-3e52</guid>
      <description>&lt;p&gt;A program is fundamentally about two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Allocating Memory:&lt;/strong&gt; Where do we store our data and variables?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parsing and Executing:&lt;/strong&gt; How do we read and run our instructions?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To run our JavaScript code, we need an engine that translates it into machine code. Browsers have different engines, like &lt;strong&gt;V8&lt;/strong&gt; in Chrome and &lt;strong&gt;SpiderMonkey&lt;/strong&gt; in Firefox. For server-side execution, we use environments like &lt;strong&gt;Node.js&lt;/strong&gt;, which is built on the V8 engine.&lt;/p&gt;

&lt;h2&gt;
  
  
  The JavaScript Engine
&lt;/h2&gt;

&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.amazonaws.com%2Fuploads%2Farticles%2F0qgpay40yvmhikkiz1gs.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.amazonaws.com%2Fuploads%2Farticles%2F0qgpay40yvmhikkiz1gs.jpg" alt="JavaScript Engine Illustration" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The JS Engine is the "heart" where your code is actually understood and executed. Inside the engine, we have two key parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memory Heap&lt;/strong&gt;: This is where memory allocation happens. When you create variables, objects, or functions, they get allocated space in the heap. This is why we need to be mindful of memory leaks. For example, by avoiding unnecessary global variables that never get cleared.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Call Stack&lt;/strong&gt;: This is where your code is read and executed, line by line. It's a stack data structure that records where we are in the program. Each function call is pushed onto the stack, and when it returns, it's popped off.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JavaScript is a &lt;strong&gt;single-threaded&lt;/strong&gt; language. This means it has only &lt;strong&gt;one Call Stack&lt;/strong&gt;. It does one thing at a time, finishing one task completely before starting the next.&lt;/p&gt;

&lt;p&gt;This single-threaded model simplifies development by avoiding the complexities of multi-threaded environments, such as deadlock scenarios common in languages like Java or C++.&lt;/p&gt;

&lt;p&gt;But this leads to a critical question...&lt;/p&gt;

&lt;h3&gt;
  
  
  The Single-Threaded Paradox: How is JavaScript Non-Blocking?
&lt;/h3&gt;

&lt;p&gt;If JavaScript has only one call stack, how can it perform asynchronous operations like waiting for a &lt;code&gt;setTimeout&lt;/code&gt; or fetching data from an API without "blocking" the main thread and freezing the UI?&lt;/p&gt;

&lt;p&gt;The answer is that the &lt;strong&gt;JavaScript Engine doesn't work alone&lt;/strong&gt;. It's part of a larger system called the &lt;strong&gt;JavaScript Runtime Environment.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The JavaScript Runtime Environment: The Orchestrator
&lt;/h2&gt;

&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.amazonaws.com%2Fuploads%2Farticles%2Fb63e7qo41kzgm06yotqa.png" 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.amazonaws.com%2Fuploads%2Farticles%2Fb63e7qo41kzgm06yotqa.png" alt="JavaScript Runtime Environment" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The runtime environment is what makes JavaScript powerful and asynchronous. It's composed of several parts working in harmony:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The JS Engine (Heap &amp;amp; Call Stack):&lt;/strong&gt; The core we just discussed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Web APIs:&lt;/strong&gt; These are features provided by the browser, like the DOM, &lt;code&gt;setTimeout&lt;/code&gt;, &lt;code&gt;fetch&lt;/code&gt; (or &lt;code&gt;XMLHttpRequest&lt;/code&gt;), and event listeners. They are not part of the JavaScript language itself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Callback Queue (or Task Queue):&lt;/strong&gt; This is a "First In, First Out" (FIFO) queue. When a background Web API task (like a timer or network request) is finished, its callback function is placed here, waiting to be run.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Event Loop:&lt;/strong&gt; This is the manager that coordinates everything. Its job is simple: "Is the Call Stack empty? If yes, take the first item from the Callback Queue and push it onto the Call Stack."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: There is also a &lt;strong&gt;Microtask Queue&lt;/strong&gt; (for Promises, like &lt;code&gt;fetch()&lt;/code&gt;, and &lt;code&gt;async/await&lt;/code&gt;) which the Event Loop checks before the Callback Queue. This is why &lt;code&gt;.then()&lt;/code&gt; or &lt;code&gt;await&lt;/code&gt; often seems to run "faster" than &lt;code&gt;setTimeout&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How Asynchronous JavaScript Works (Step-by-Step)
&lt;/h3&gt;

&lt;p&gt;Let's trace what happens when we run an asynchronous function like setTimeout:&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="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="s1"&gt;Start&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&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;myCallback&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="s1"&gt;Inside Timeout.&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="mi"&gt;1000&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="s1"&gt;End&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;ol&gt;
&lt;li&gt;
&lt;code&gt;console.log('Start')&lt;/code&gt; is pushed onto the &lt;strong&gt;Call Stack&lt;/strong&gt;. It runs, prints "Start", and is popped off.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setTimeout(...)&lt;/code&gt; is pushed onto the &lt;strong&gt;Call Stack&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setTimeout&lt;/code&gt; is recognized as &lt;strong&gt;a Web API&lt;/strong&gt;. The engine tells the browser to "start a 1-second timer. When you're done, take &lt;code&gt;myCallback&lt;/code&gt; and put it in the queue."&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;setTimeout&lt;/code&gt; call itself finishes immediately (its only job was to hand off the timer to the browser). It is &lt;strong&gt;popped off the Call Stack&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log('End')&lt;/code&gt; is pushed onto the &lt;strong&gt;Call Stack&lt;/strong&gt;. It runs, prints "End", and is popped off.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Call Stack is now empty&lt;/strong&gt;. The main script is finished.&lt;/li&gt;
&lt;li&gt;...In the background, the browser's timer is still ticking...&lt;/li&gt;
&lt;li&gt;After 1 second, the timer finishes. The browser takes &lt;code&gt;myCallback&lt;/code&gt; and places it in the &lt;strong&gt;Callback Queue&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Event Loop&lt;/strong&gt; is always checking. It sees the &lt;strong&gt;Call Stack is empty&lt;/strong&gt; and there's something in the &lt;strong&gt;Callback Queue&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The Event Loop moves myCallback from the Callback Queue to the Call Stack.&lt;/li&gt;
&lt;li&gt;The engine executes &lt;code&gt;myCallback&lt;/code&gt; code. &lt;code&gt;console.log('Inside Timeout.')&lt;/code&gt; is pushed to the stack, prints "Inside Timeout", and is popped.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;myCallback&lt;/code&gt; finishes and is popped. The Call Stack is empty. The program is complete.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Output:&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;Start
End
Inside Timeout.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Recap: The Power of the Runtime
&lt;/h2&gt;

&lt;p&gt;JavaScript is a single-threaded language, meaning it has only one Call Stack and can execute only one task at a time. It achieves non-blocking behavior by delegating slow operations - like timers or API calls - to the browser's Web APIs, which handle them in the background. Once a background task completes, its callback function is placed in the Callback Queue. The Event Loop constantly monitors the Call Stack, and the moment it becomes empty, the Event Loop takes the first callback from the queue and pushes it onto the stack for execution. This mechanism ensures the main thread is never blocked.&lt;/p&gt;

&lt;p&gt;Thank you for reading! If you found this article helpful, please give it a ❤️ and 🦄! Feel free to share your thoughts in the comments, and let's connect on &lt;a href="https://www.linkedin.com/in/adelstiti/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; — I'd love to hear your insights!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
