<?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: Aman Goel</title>
    <description>The latest articles on DEV Community by Aman Goel (@amangoel_dev).</description>
    <link>https://dev.to/amangoel_dev</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%2F2957909%2F721dcf4c-3103-474b-b790-e8e739a6d4a5.png</url>
      <title>DEV Community: Aman Goel</title>
      <link>https://dev.to/amangoel_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amangoel_dev"/>
    <language>en</language>
    <item>
      <title>How JavaScript Executes Code: Synchronous vs Asynchronous Execution</title>
      <dc:creator>Aman Goel</dc:creator>
      <pubDate>Thu, 20 Mar 2025 15:14:20 +0000</pubDate>
      <link>https://dev.to/amangoel_dev/how-javascript-executes-code-synchronous-vs-asynchronous-execution-4k29</link>
      <guid>https://dev.to/amangoel_dev/how-javascript-executes-code-synchronous-vs-asynchronous-execution-4k29</guid>
      <description>&lt;p&gt;Javascript is a single-threaded language that executes one task at a time. However, it handles the asynchronous tasks very efficiently using the Web APIs, callback queues, and the event loop.&lt;br&gt;
This blog explains how JavaScript executes code synchronously and asynchronously, including the event loop mechanism.&lt;/p&gt;
&lt;h2&gt;
  
  
  Synchronous Execution in Javascript
&lt;/h2&gt;

&lt;p&gt;javascript executes the code line by line following the LIFO approach in the call stack.&lt;br&gt;
The call stack keeps track of the function calls.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function greet() {
  console.log("Hello");
}

function ask() {
  console.log("How are you?");
}

function bye() {
  console.log("Goodbye!");
}

greet();
ask();
bye();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see this in the above code block. First, we made a function call for greet(), then for ask(), and then for a bye(). The output we get is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello
How are you?
Goodbye!

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output shows us that Execution is happening line by line.&lt;br&gt;
First, the greet function came to the call stack and executed itself, and then it popped out from the call stack.&lt;/p&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%2Fhufu3w8bbw8oplx32enb.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%2Fhufu3w8bbw8oplx32enb.png" alt="Image description" width="338" height="525"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After this, the other two functions came to the call stack one by one, got executed, and popped out.&lt;/p&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%2Fwnszvaoybode7ms5zf8j.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%2Fwnszvaoybode7ms5zf8j.png" alt="Image description" width="330" height="517"&gt;&lt;/a&gt;&lt;/p&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%2Fq5tjgfk3fixpki8rof9w.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%2Fq5tjgfk3fixpki8rof9w.png" alt="Image description" width="341" height="507"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the above code and picture, we are transparent about the code synchronization in JS. Now, let's take a look at asynchronous Execution.&lt;/p&gt;
&lt;h2&gt;
  
  
  Asynchronous Execution
&lt;/h2&gt;

&lt;p&gt;AsynchronExecutiontion means that JavaScript does not wait for a task to complete before moving on to the next one. Instead, it delegates certain operations (like network requests or timers) to the Web APIs, allowing the main program to continue running.&lt;/p&gt;

&lt;p&gt;We can understand it using the analogy of Maggie cooking. &lt;br&gt;
For cooking, Maggiee's steps are -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Boiling the water (4 mins)&lt;/li&gt;
&lt;li&gt;cutting vegetables (5 mins)&lt;/li&gt;
&lt;li&gt;Make Maggie (2min)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you boil water for the maggie, you don't wait for it to cook; when it is boiled, you start cutting the vegetables. Instead, as you put the water to boil, you start to cut the vegetables and check whether the water is boiled. After cutting the vegetables, turn off the gas from the boiling water and make the maggie.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Boiling water → A long-running asynchronous task (setTimeout, fetch).&lt;/li&gt;
&lt;li&gt;Cutting vegetables while waiting for water to boil → Other synchronous code executing without waiting.&lt;/li&gt;
&lt;li&gt;Checking if the water is boiled → The Event Loop checking if the async task is complete.&lt;/li&gt;
&lt;li&gt;Turning off the gas and making Maggi →. Executing the callback once the async task (boiling) is done.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The event-loop callback queue will be explained further in the blog.&lt;/p&gt;

&lt;p&gt;Let's take a look at the Asynchornus code and its output -&amp;gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("Start");

setTimeout(() =&amp;gt; {
  console.log("Inside setTimeout");
}, 2000);

console.log("End");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the above code block, the output will be&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 setTimeout

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside setTimeout is between the Start and End, but it is printed at the last because of the asynchronous function &lt;strong&gt;setTimeout&lt;/strong&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Behind the scenes:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;"Start" is logged immediately.&lt;/li&gt;
&lt;li&gt;setTimeout() is encountered. Instead of blocking Execution, it delegates 
the task to the Web API and sets a timer for 2 seconds.&lt;/li&gt;
&lt;li&gt;"End" is logged immediately because JavaScript does not wait for 
setTimeout() to finish.&lt;/li&gt;
&lt;li&gt;After 2 seconds, the callback function inside setTimeout() is added to 
the callback queue.&lt;/li&gt;
&lt;li&gt;The event loop checks the call stack. Once it is empty, the callback 
function is executed, logging "Inside setTimeout."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To observe the Execution of the code in js, you can go to this site &lt;a href="http://latentflip.com/loupe/?code=JC5vbignYnV0dG9uJywgJ2NsaWNrJywgZnVuY3Rpb24gb25DbGljaygpIHsKICAgIHNldFRpbWVvdXQoZnVuY3Rpb24gdGltZXIoKSB7CiAgICAgICAgY29uc29sZS5sb2coJ1lvdSBjbGlja2VkIHRoZSBidXR0b24hJyk7ICAgIAogICAgfSwgMjAwMCk7Cn0pOwoKY29uc29sZS5sb2coIkhpISIpOwoKc2V0VGltZW91dChmdW5jdGlvbiB0aW1lb3V0KCkgewogICAgY29uc29sZS5sb2coIkNsaWNrIHRoZSBidXR0b24hIik7Cn0sIDUwMDApOwoKY29uc29sZS5sb2coIldlbGNvbWUgdG8gbG91cGUuIik7!!!PGJ1dHRvbj5DbGljayBtZSE8L2J1dHRvbj4%3D" rel="noopener noreferrer"&gt;click here&lt;/a&gt;&lt;/p&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%2Fz0xc5ooigjsw2o4565zg.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%2Fz0xc5ooigjsw2o4565zg.png" alt="Image description" width="800" height="357"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Synchronous Execution follows a linear, blocking approach.&lt;/li&gt;
&lt;li&gt;Asynchronous Execution allows JavaScript to perform other tasks while 
waiting for slow operations to complete.
-The event loop ensures that asynchronous tasks execute only when the call stack is clear.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>[Boost]</title>
      <dc:creator>Aman Goel</dc:creator>
      <pubDate>Thu, 20 Mar 2025 13:34:04 +0000</pubDate>
      <link>https://dev.to/amangoel_dev/-3ijo</link>
      <guid>https://dev.to/amangoel_dev/-3ijo</guid>
      <description></description>
      <category>emptystring</category>
    </item>
  </channel>
</rss>
