<?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: Ravi</title>
    <description>The latest articles on DEV Community by Ravi (@ravijabade12).</description>
    <link>https://dev.to/ravijabade12</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%2F1617727%2F28868a11-bb89-4e66-af6c-3bcc05c854f8.png</url>
      <title>DEV Community: Ravi</title>
      <link>https://dev.to/ravijabade12</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ravijabade12"/>
    <language>en</language>
    <item>
      <title>How JavaScript Works</title>
      <dc:creator>Ravi</dc:creator>
      <pubDate>Mon, 10 Mar 2025 01:35:37 +0000</pubDate>
      <link>https://dev.to/ravijabade12/how-javascript-works-2a12</link>
      <guid>https://dev.to/ravijabade12/how-javascript-works-2a12</guid>
      <description>&lt;h2&gt;
  
  
  How JavaScript Works
&lt;/h2&gt;

&lt;p&gt;JavaScript is a &lt;strong&gt;synchronous, single-threaded&lt;/strong&gt; programming language, meaning it executes code sequentially, one operation at a time, within a single execution thread. To understand how JavaScript processes code, we need to explore its &lt;strong&gt;Execution Context&lt;/strong&gt; and the &lt;strong&gt;Call Stack&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;Everything in JavaScript happens inside an &lt;strong&gt;Execution Context&lt;/strong&gt;. When we run a JavaScript program, a new &lt;strong&gt;Global Execution Context (GEC)&lt;/strong&gt; is created. This context consists of two phases:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Memory Creation Phase
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;All &lt;strong&gt;variables&lt;/strong&gt; and &lt;strong&gt;functions&lt;/strong&gt; are stored in memory as key-value pairs.&lt;/li&gt;
&lt;li&gt;Variables are assigned a special placeholder value: &lt;strong&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Function declarations are stored as &lt;strong&gt;entire function definitions&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&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;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Undefined&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&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;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, World!&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before executing the code, JavaScript sets up memory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Memory:
x -&amp;gt; undefined
greet -&amp;gt; function definition
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Code Execution Phase
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Code is executed &lt;strong&gt;line by line&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Variables are assigned actual values.&lt;/li&gt;
&lt;li&gt;When a function is invoked, a new &lt;strong&gt;Function Execution Context (FEC)&lt;/strong&gt; is created.&lt;/li&gt;
&lt;li&gt;Once the function finishes execution, its &lt;strong&gt;execution context is deleted&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Example:
&lt;/h4&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;add&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="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Global Execution Context (GEC) is created&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory is allocated&lt;/strong&gt; for &lt;code&gt;add&lt;/code&gt; and &lt;code&gt;result&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When &lt;code&gt;add(5,3)&lt;/code&gt; is called, a &lt;strong&gt;Function Execution Context (FEC)&lt;/strong&gt; is created.&lt;/li&gt;
&lt;li&gt;Once the function returns a value, its &lt;strong&gt;context is removed&lt;/strong&gt; from memory.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;The &lt;strong&gt;Call Stack&lt;/strong&gt; is a data structure that manages execution contexts.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When a function is invoked, it is &lt;strong&gt;pushed&lt;/strong&gt; onto the stack.&lt;/li&gt;
&lt;li&gt;When the function completes, it is &lt;strong&gt;popped&lt;/strong&gt; off the stack.&lt;/li&gt;
&lt;li&gt;Once all functions finish execution, the &lt;strong&gt;Global Execution Context (GEC) is removed&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  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;first&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;First function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;second&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;First function again&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;second&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;Second function&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;first&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Call Stack Execution:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. GEC is created
2. first() is called -&amp;gt; Pushed onto the stack
3. "First function" is logged
4. second() is called -&amp;gt; Pushed onto the stack
5. "Second function" is logged, second() completes -&amp;gt; Popped off the stack
6. "First function again" is logged, first() completes -&amp;gt; Popped off the stack
7. GEC is removed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Final Output:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;First function
Second function
First function again
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;JavaScript is synchronous and single-threaded.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Execution Context&lt;/strong&gt; has two phases: &lt;strong&gt;Memory Creation&lt;/strong&gt; and &lt;strong&gt;Code Execution&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Call Stack&lt;/strong&gt; maintains function execution order.&lt;/li&gt;
&lt;li&gt;When execution finishes, the &lt;strong&gt;Global Execution Context is removed&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding these fundamentals is key to mastering JavaScript, especially when dealing with asynchronous operations like callbacks, promises, and the event loop. 🚀&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>frontend</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
