<?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: Suriya Tasmim Disha</title>
    <description>The latest articles on DEV Community by Suriya Tasmim Disha (@suriyadisha).</description>
    <link>https://dev.to/suriyadisha</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%2F387283%2F7b1c4cf4-c96a-471e-9db5-80463b17f387.jpeg</url>
      <title>DEV Community: Suriya Tasmim Disha</title>
      <link>https://dev.to/suriyadisha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/suriyadisha"/>
    <language>en</language>
    <item>
      <title>JavaScript Callbacks Made Easy: Learn, Apply &amp; Conquer! 🚀</title>
      <dc:creator>Suriya Tasmim Disha</dc:creator>
      <pubDate>Mon, 19 Feb 2024 19:22:03 +0000</pubDate>
      <link>https://dev.to/suriyadisha/javascript-callbacks-made-easy-learn-apply-conquer-346d</link>
      <guid>https://dev.to/suriyadisha/javascript-callbacks-made-easy-learn-apply-conquer-346d</guid>
      <description>&lt;p&gt;Have you ever stumbled upon functions within functions in your JavaScript journey? These are known as callback functions, and they play a crucial role in asynchronous programming. In this blog, we'll delve into the world of callbacks, making them clear and approachable for beginners.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are Callback Functions?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine you're passing a message to a busy friend. You wouldn't want to wait for them to finish everything before delivering it. Similarly, in JavaScript, callback functions are "messages" you send to functions that might take time to complete their tasks. These functions then "call back" to your code later when they're done.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating and Using Callbacks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's see how it works in practice:&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;function&lt;/span&gt; &lt;span class="nf"&gt;showResult&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;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;span class="p"&gt;}&lt;/span&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;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Send the result to the "callback" function&lt;/span&gt;
&lt;span class="p"&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;4&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="nx"&gt;showResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Pass "showResult" as the callback&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;add&lt;/code&gt; takes three arguments: &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;, and &lt;code&gt;callback&lt;/code&gt;. It calculates the sum and then "calls back" to &lt;code&gt;showResult&lt;/code&gt; by passing the sum as an argument. This allows you to handle the result in a separate function, making your code more modular and organized.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Synchronous vs. Asynchronous Callbacks&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So far, we've seen synchronous callbacks, where the "calling back" happens immediately after the task is done. But callbacks truly shine in asynchronous scenarios, where a function might need to wait for something (like fetching data from the internet) before completing.&lt;/p&gt;

&lt;p&gt;Here's an example:&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;function&lt;/span&gt; &lt;span class="nf"&gt;showResult&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;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="o"&gt;=&amp;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;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;// Output: 9 after 5 seconds&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="c1"&gt;// Simulate a 5 seconds delay&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;add&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="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;callback&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;span class="c1"&gt;// Send the result to the "callback" function&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;Hi! There!&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&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="nx"&gt;showResult&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Pass "showResult" as the callback&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, &lt;code&gt;showResult&lt;/code&gt; uses &lt;code&gt;setTimeout&lt;/code&gt; to simulate an asynchronous operation, and then it calls back to the provided function with the data after a delay. This allows your code to continue execution without being blocked while waiting for the data.&lt;/p&gt;

&lt;p&gt;Watch my video to learn more about callbacks and how you can use these in a real-life application! 🎥&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/VjactTwqE2Y"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Benefits of Callbacks&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Asynchronous Programming:&lt;/strong&gt; Callbacks enable your code to handle tasks that take time without blocking the main thread. This makes your web applications more responsive and interactive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Modularization:&lt;/strong&gt; You can break down complex logic into smaller, reusable functions, making your code easier to read, understand, and maintain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flexibility:&lt;/strong&gt; Callbacks can be passed around and used in various contexts, making your code more adaptable.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Beyond the Basics: Advanced Concepts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;While callbacks are powerful, they can also lead to "callback hell" in complex scenarios. To overcome this, JavaScript offers alternative approaches like &lt;strong&gt;promises&lt;/strong&gt; and &lt;strong&gt;async/await&lt;/strong&gt;, which provide cleaner and more readable ways to handle asynchronous operations. We'll explore these in future posts!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Always pass callback functions without parentheses (&lt;code&gt;add(5, 10, showResult)&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Callbacks can be used for both synchronous and asynchronous operations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;They offer significant benefits for writing modular, asynchronous code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope this blog has clarified callback functions for you! Feel free to ask any questions in the comments, and I'll be happy to help.&lt;/p&gt;

&lt;p&gt;Happy learning! 💻 😄&lt;/p&gt;

&lt;p&gt;🌐 GitHub Link: &lt;a href="https://github.com/SuriyaTasmimDisha/JavaScript-Callbacks.git"&gt;https://github.com/SuriyaTasmimDisha/JavaScript-Callbacks.git&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow me and support me on:&lt;/p&gt;

&lt;p&gt;🔗 LinkedIn: &lt;a href="https://www.linkedin.com/in/suriya-ta"&gt;&lt;strong&gt;https://www.linkedin.com/in/suriya-ta&lt;/strong&gt;&lt;/a&gt;...&lt;/p&gt;

&lt;p&gt;🐦 Twitter: &lt;a href="https://twitter.com/SuriyaDisha"&gt;&lt;strong&gt;https://twitter.com/SuriyaDisha&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📝 Tech Blog: &lt;a href="https://blog.suriyadisha.com/"&gt;&lt;strong&gt;https://blog.suriyadisha.com&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;☕ Buy me a coffee: &lt;a href="https://www.buymeacoffee.com/suriyadisha"&gt;&lt;strong&gt;https://www.buymeacoffee.com/suriyadisha&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>callbacks</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Master Asynchronous JavaScript Like a Pro (Easy Guide!)</title>
      <dc:creator>Suriya Tasmim Disha</dc:creator>
      <pubDate>Mon, 12 Feb 2024 17:17:17 +0000</pubDate>
      <link>https://dev.to/suriyadisha/master-asynchronous-javascript-like-a-pro-easy-guide-pgg</link>
      <guid>https://dev.to/suriyadisha/master-asynchronous-javascript-like-a-pro-easy-guide-pgg</guid>
      <description>&lt;p&gt;JavaScript is a powerful tool for web development, but its synchronous nature can sometimes slow things down. Enter &lt;strong&gt;Asynchronous&lt;/strong&gt; JavaScript, your key to unlocking faster, smoother code!&lt;/p&gt;

&lt;p&gt;The word &lt;strong&gt;&lt;em&gt;asynchronous&lt;/em&gt;&lt;/strong&gt; means not occurring at the same time. But in JavaScript, the code gets executed line by line as it has a single thread. So, JavaScript is synchronous by nature.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So, how do we make JavaScript act asynchronously?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To make JavaScript work asynchronously, we first need to understand how the JavaScript code executes.&lt;/p&gt;

&lt;p&gt;To run a javascript script we need a &lt;strong&gt;JavaScript Engine&lt;/strong&gt;. The purpose of the javascript engine is to translate the code developers write into machine code. The computer reads this machine code and performs the specified tasks.&lt;/p&gt;

&lt;p&gt;So, when the JavaScript engine starts executing the code it creates &lt;strong&gt;Execution Context&lt;/strong&gt;. Each execution context has two phases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first one is the &lt;strong&gt;Creation phase&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second one is the &lt;strong&gt;Execution phase&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the creation phase, JavaScript creates a &lt;strong&gt;global execution context&lt;/strong&gt;. Then it creates a &lt;strong&gt;'this'&lt;/strong&gt; object and binds it to the global object. After this, it creates a &lt;strong&gt;memory heap&lt;/strong&gt; to store variables and function references. Next, it initializes the variable within the global execution context to &lt;strong&gt;undefined&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the execution phase, the javascript engine assigns values to the variables and executes the function calls. For each function call, the javascript engine creates a new function execution context. It is very similar to the global execution context. But instead of creating a global object, it creates an arguments object. This argument object is a reference to all the parameters of the function.&lt;/p&gt;

&lt;p&gt;To understand this workflow more clearly, let’s take a look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&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;2&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&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;c&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&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;b&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="nx"&gt;a&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;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, this is a simple javascript code where we have 3 variables and one function.&lt;/p&gt;

&lt;p&gt;When we run this script, the JavaScript engine first goes into the creation phase and creates a global execution context. Then it creates a global object. As we use vanilla JavaScript, this global object is a window that we get from the web browser. Then it creates &lt;strong&gt;this&lt;/strong&gt; object and binds it to the &lt;strong&gt;window&lt;/strong&gt; object. Next, it creates a memory heap in the global execution context. There in the memory heap, it stores the variable &lt;strong&gt;a&lt;/strong&gt;, &lt;strong&gt;b,&lt;/strong&gt; and the declaration of the &lt;code&gt;add()&lt;/code&gt; function. After that, it initializes the variables &lt;strong&gt;a&lt;/strong&gt; and &lt;strong&gt;b&lt;/strong&gt; to &lt;strong&gt;undefined&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqxg05e265v8n301jjy11.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqxg05e265v8n301jjy11.gif" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After this, the engine goes into the execution phase.&lt;/p&gt;

&lt;p&gt;In the execution phase, it first assigns values to the variables and executes function calls. For executing and keeping track of the function calls, the javascript engine maintains a call stack and there is only one call stack available in the web browser. The javascript engine places &lt;code&gt;main()&lt;/code&gt; function from the global execution context to the call stack.&lt;/p&gt;

&lt;p&gt;Next, for the &lt;code&gt;main()&lt;/code&gt; function, the engine enters the creation phase. After completing the creation phase, it then moves to the execution phase.&lt;/p&gt;

&lt;p&gt;Then javascript engine executes the call to the &lt;code&gt;add()&lt;/code&gt; function and creates a function execution context with an argument object. This argument object keeps the references of all parameters passed into the function. Next, it sets &lt;strong&gt;this&lt;/strong&gt; value to the global object and initializes parameter &lt;strong&gt;c&lt;/strong&gt; to &lt;strong&gt;undefined.&lt;/strong&gt; Then it pushes the &lt;code&gt;add()&lt;/code&gt; function on top of the call stack.&lt;/p&gt;

&lt;p&gt;After this, the engine executes the &lt;code&gt;add()&lt;/code&gt; function on top of the call stack. During this execution phase, the engine assigns &lt;strong&gt;2&lt;/strong&gt; to the parameter &lt;strong&gt;c&lt;/strong&gt; and returns the result &lt;strong&gt;4&lt;/strong&gt; to the global execution context. Next, it pops the &lt;code&gt;add()&lt;/code&gt; function from the call stack.&lt;/p&gt;

&lt;p&gt;In the final stage of the execution context, the return value from the &lt;code&gt;add()&lt;/code&gt; function is assigned to variable &lt;strong&gt;b&lt;/strong&gt;. Then the value of &lt;strong&gt;b&lt;/strong&gt; is printed in the console. With this, the &lt;code&gt;main()&lt;/code&gt; function popped off from the call stack completing all the execution contexts and the script stopped running.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgunt4tk9h2t2mshyq6rc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgunt4tk9h2t2mshyq6rc.gif" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, as you can see with javascript we are doing one step at a time.&lt;/p&gt;

&lt;p&gt;Now let’s look at another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="mi"&gt;1&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="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1024&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this code, we want to print &lt;strong&gt;1&lt;/strong&gt;, the &lt;strong&gt;square root of 1024&lt;/strong&gt;, and &lt;strong&gt;2&lt;/strong&gt; in the console. So, this is another simple example of the synchronous javascript execution. However, there is a catch. Here, we are calculating the square root of a number. Now the bigger the number the more time it will take to execute. So, other operations will be on hold till then. This situation is called &lt;strong&gt;&lt;em&gt;Blocking&lt;/em&gt;&lt;/strong&gt; and it is one of the major drawbacks of synchronous javascript.&lt;/p&gt;

&lt;p&gt;To solve this situation we are going to implement an asynchronous mechanism. As you have read in the beginning asynchronous mechanism allows us to work on multiple things at a time.&lt;/p&gt;

&lt;p&gt;So, to implement the asynchronous mechanism in JavaScript, we need to understand some new and interesting mechanisms of our web browsers. Our web browsers contain a &lt;strong&gt;JavaScript Runtime Environment.&lt;/strong&gt; This runtime environment consists of a &lt;strong&gt;JavaScript Engine&lt;/strong&gt;, &lt;strong&gt;Web APIs&lt;/strong&gt;, an &lt;strong&gt;Event Loop&lt;/strong&gt;, and a &lt;strong&gt;CallBack Queue&lt;/strong&gt;. We are already familiar with the javascript engine. Now, let’s get to know the other three.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fii7qdyqm4mag92c916kv.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fii7qdyqm4mag92c916kv.gif" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Web APIs consist of various kinds of modern APIs like event listeners, timing functions, and AJAX requests. We use these APIs to manipulate the HTML, and CSS of the web pages, draw and modify graphics, and get data from the network servers.&lt;/p&gt;

&lt;p&gt;The CallBack Queue stores the callback functions sent from the Web APIs. The queue stores these functions in first in first out order.&lt;/p&gt;

&lt;p&gt;The Event Loop continuously monitors the state of the call stack and callback queue. When the call stack gets empty, it takes a callback function from the callback queue and puts it onto the call stack.&lt;/p&gt;

&lt;p&gt;So, if you notice javascript is still executing synchronously. But because of using the JavaScript Engine, Web APIs, an Event Loop, and a CallBack Queue, it makes us think that javascript is executing asynchronously.&lt;/p&gt;

&lt;p&gt;Now let’s take a look at an example to understand this whole flow. In this example, I have modified our previous code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="mi"&gt;1&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="o"&gt;=&amp;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="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;3000&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time, in the code I have introduced a new method named &lt;strong&gt;setTimeout&lt;/strong&gt;. This &lt;code&gt;setTimeout()&lt;/code&gt; is a timing function provided by the Web APIs. This method sets a timer that executes a function or specified piece of code once the timer expires.&lt;/p&gt;

&lt;p&gt;So, our code first prints &lt;strong&gt;1&lt;/strong&gt; to the console.&lt;/p&gt;

&lt;p&gt;Then it moves to the &lt;code&gt;setTimeout()&lt;/code&gt; method. Here we have specified the square root code and a delay of 3000ms. That means this block of code will get executed after 3s.&lt;/p&gt;

&lt;p&gt;Next, this method is moved from the call stack to the Callback Queue via Web APIs, and in the Callback Queue, the method processing keeps on running.&lt;/p&gt;

&lt;p&gt;Then, the execution pointer moves to the third line and prints &lt;strong&gt;2&lt;/strong&gt; to the console.&lt;/p&gt;

&lt;p&gt;Now, as our call stack is empty, the event loop looks into the Callback Queue and takes the &lt;strong&gt;setTimeout&lt;/strong&gt; method to the call stack. By this time, if 3 seconds is up it prints the square root of the given number in the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwelmi7epi4h7gi7enybw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwelmi7epi4h7gi7enybw.gif" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, this is how asynchronous javascript works while not creating any blocking.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/UQYBn2qeZ4c"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Wrapping Up&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;That's it. Thanks for stopping by. I hope by now you have a good understanding of what asynchronous javascript is and how it works.&lt;/p&gt;

&lt;p&gt;My DMs are always open if you want to discuss further on any tech topic or if you've got any questions, suggestions, or feedback in general.&lt;/p&gt;

&lt;p&gt;Happy learning! 💻 😄&lt;/p&gt;

&lt;p&gt;Follow me and support me on:&lt;/p&gt;

&lt;p&gt;🔗 LinkedIn: &lt;a href="https://www.linkedin.com/in/suriya-ta"&gt;https://www.linkedin.com/in/suriya-ta&lt;/a&gt;...&lt;/p&gt;

&lt;p&gt;🐦 Twitter: &lt;a href="https://twitter.com/SuriyaDisha"&gt;https://twitter.com/SuriyaDisha&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;📝 Tech Blog: &lt;a href="https://blog.suriyadisha.com/"&gt;https://blog.suriyadisha.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;☕ Buy me a coffee: &lt;a href="https://www.buymeacoffee.com/suriyadisha"&gt;https://www.buymeacoffee.com/suriyadisha&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript 101: A Beginner's Guide To The Call Stack</title>
      <dc:creator>Suriya Tasmim Disha</dc:creator>
      <pubDate>Sun, 04 Feb 2024 18:36:43 +0000</pubDate>
      <link>https://dev.to/suriyadisha/javascript-101-a-beginners-guide-to-the-call-stack-2hc</link>
      <guid>https://dev.to/suriyadisha/javascript-101-a-beginners-guide-to-the-call-stack-2hc</guid>
      <description>&lt;p&gt;A &lt;strong&gt;Call Stack&lt;/strong&gt; is a mechanism for the interpreter to keep track of which function is running right now and which function needs to run next.&lt;/p&gt;

&lt;p&gt;JavaScript is a &lt;strong&gt;single-threaded&lt;/strong&gt; programming language. That means it can do one thing at a time and has only one Call Stack at its disposal. The Call Stack uses the &lt;strong&gt;Last In First Out (LIFO)&lt;/strong&gt; principle to store and manage function calls temporarily.&lt;/p&gt;

&lt;p&gt;To understand LIFO, let’s say we bought the Harry Potter collection. Now, we are going to store them in order. We will first stack part 7 as we will read it at last. Then part 6, part 5, part 4, part 3, part 2, and at the top part 1. We store the sequence of each book in a chunk of memory. This chunk of memory is called a &lt;strong&gt;Stack Frame&lt;/strong&gt;. We can have several Stack Frames exist at a time but only one Stack Frame can be active. That means you can read one book at a time.&lt;/p&gt;

&lt;p&gt;When we will start reading, we will first take part 1 which is the active Stack Frame from the stack. So, this frees up one Stack Frame. After finishing part 1, we will take part 2 which clears up the 2nd Stack Frame. Similarly, we then take out part 3, part 4, part 5, part 6, and finally part 7. And now our Call Stack is empty. So, to process the elements or requests starting from the last to the first is the principle of the Last In First Out method.&lt;/p&gt;

&lt;p&gt;In programming the last function that gets pushed into the stack is the first to be popped out when the function returns.&lt;/p&gt;

&lt;p&gt;To better understand the Call Stack, let’s look at an example. Let's open &lt;strong&gt;Google Chrome&lt;/strong&gt; and open the &lt;strong&gt;inspection&lt;/strong&gt; tool. In the inspection tool, select the &lt;strong&gt;Sources&lt;/strong&gt; tab. Here, let’s select the &lt;strong&gt;Snippets&lt;/strong&gt; module. Now let’s create a &lt;strong&gt;new snippet&lt;/strong&gt;. In this snippet, let’s write a simple code in JavaScript.&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;function&lt;/span&gt; &lt;span class="nf"&gt;sayHello&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;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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
&lt;span class="nf"&gt;sayHello&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;Hello 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="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are going to execute this code and run the debugger so that we can see how the interpreter handles multiple function calls using the Call Stack.&lt;/p&gt;

&lt;p&gt;So, let’s a put pointer on the &lt;code&gt;greeting()&lt;/code&gt; function and hit the &lt;code&gt;command + enter&lt;/code&gt; key to start the execution. As you can see there is a brand new function called &lt;strong&gt;anonymous&lt;/strong&gt; in the call stack list. This is our &lt;strong&gt;main&lt;/strong&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffq9ncck993cx6mmkj449.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffq9ncck993cx6mmkj449.png" alt="Image description" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, when we start executing this code the &lt;strong&gt;main&lt;/strong&gt; function gets called first. A stack frame is assigned to the &lt;strong&gt;main&lt;/strong&gt; function. So that the &lt;strong&gt;main&lt;/strong&gt; function gets added to the Call Stack and does its work.&lt;/p&gt;

&lt;p&gt;Next, let’s click on the &lt;strong&gt;step (⬇)&lt;/strong&gt; button. This action makes the &lt;strong&gt;main&lt;/strong&gt; function call the &lt;code&gt;greeting()&lt;/code&gt; function and the &lt;code&gt;greeting()&lt;/code&gt; function gets added to the Call Stack with a Stack Frame. This is our current active Stack Frame while the previous one is on hold.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqbesgcvlrnld393cb83m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqbesgcvlrnld393cb83m.png" alt="Image description" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that let’s hit the &lt;strong&gt;step (⬇)&lt;/strong&gt; button once again. This time the &lt;code&gt;greeting()&lt;/code&gt; function calls the &lt;code&gt;sayHello()&lt;/code&gt; function and it gets added to the Call Stack with a new active Stack Frame.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fodrizldx4xfmba7hwbfo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fodrizldx4xfmba7hwbfo.png" alt="Image description" width="800" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After pressing the &lt;strong&gt;step (⬇)&lt;/strong&gt; button one more time, the &lt;strong&gt;sayHello()&lt;/strong&gt; function prints &lt;code&gt;"Hello"&lt;/code&gt; in the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhuull12h8jwit9jpdgq9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhuull12h8jwit9jpdgq9.png" alt="Image description" width="800" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again pressing the &lt;strong&gt;step (⬇)&lt;/strong&gt; button, the &lt;code&gt;sayHello()&lt;/code&gt; function returns, and the Stack Frame is popped off from the Call Stack. The execution order then moves below the stack and the immediate frame which is the &lt;code&gt;greeting()&lt;/code&gt; function gets activated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5mz0emh6zucg8naagof5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5mz0emh6zucg8naagof5.png" alt="Image description" width="800" height="365"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, let’s press the &lt;strong&gt;step&lt;/strong&gt; &lt;strong&gt;(⬇)&lt;/strong&gt; button again. This time the content of the &lt;code&gt;greeting()&lt;/code&gt; function executes and prints &lt;code&gt;"Hello again!"&lt;/code&gt; in the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6w2d042ezbxzd4kzcu3k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6w2d042ezbxzd4kzcu3k.png" alt="Image description" width="800" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s hit the &lt;strong&gt;step (⬇)&lt;/strong&gt; button again. Now, the &lt;code&gt;greeting()&lt;/code&gt; function returns, and once again the Stack Frame is popped off from the Call Stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg8i0bmslpvowztqq6gby.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg8i0bmslpvowztqq6gby.png" alt="Image description" width="800" height="376"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we just have the &lt;strong&gt;main&lt;/strong&gt; function in our Call Stack. So, let's press the step button one last time and with this action Call Stack becomes empty.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now why Call Stack is so important to understand?!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To understand this, let’s go back to the &lt;strong&gt;inspection&lt;/strong&gt; tool and modify our code a little.&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;function&lt;/span&gt; &lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
&lt;span class="nf"&gt;greeting&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;greeting&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
&lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Now let’s hit &lt;code&gt;command + enter&lt;/code&gt; to run this updated code. And we get an error in the console saying &lt;code&gt;"Maximum call stack size exceeded"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4qfek9s7eopzmfylayml.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4qfek9s7eopzmfylayml.png" alt="Image description" width="800" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For JavaScript, our browser is responsible for executing the code and it has a &lt;strong&gt;limited amount of memory&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the current code, we are calling the &lt;code&gt;sayHello()&lt;/code&gt; function from the &lt;code&gt;greeting()&lt;/code&gt; function. Then from the &lt;code&gt;sayHello()&lt;/code&gt; function, we are calling the &lt;code&gt;greeting()&lt;/code&gt; function. Next from the &lt;code&gt;greeting()&lt;/code&gt; function we are once again calling the &lt;code&gt;sayHello()&lt;/code&gt; function and so on. So, an infinite loop has been created. However, we don’t have infinite memory in the browser. So, when the maximum amount of stack frame is exceeded, the browser throws the &lt;code&gt;"Maximum call stack size exceeded"&lt;/code&gt; error.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/tn3YjekKVOU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Wrapping Up&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;That's it. Thanks for stopping by. I hope you found this article interesting and informative!&lt;/p&gt;

&lt;p&gt;My DMs are always open if you want to discuss further on any tech topic or if you've got any questions, suggestions, or feedback in general.&lt;/p&gt;

&lt;p&gt;Happy learning! 💻 😄&lt;/p&gt;

&lt;p&gt;Follow me and support me on:&lt;br&gt;
🔗 LinkedIn: &lt;a href="https://www.linkedin.com/in/suriya-ta"&gt;https://www.linkedin.com/in/suriya-ta&lt;/a&gt;...&lt;br&gt;
🐦 Twitter: &lt;a href="https://twitter.com/SuriyaDisha"&gt;https://twitter.com/SuriyaDisha&lt;/a&gt;&lt;br&gt;
📝 Tech Blog: &lt;a href="https://blog.suriyadisha.com"&gt;https://blog.suriyadisha.com&lt;/a&gt;&lt;br&gt;
☕ Buy me a coffee: &lt;a href="https://www.buymeacoffee.com/suriyadisha"&gt;https://www.buymeacoffee.com/suriyadisha&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>callstack</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Full Stack 🤖 AI-Powered 📧 Email Generation App using OpenAI API - Part 1</title>
      <dc:creator>Suriya Tasmim Disha</dc:creator>
      <pubDate>Tue, 15 Aug 2023 13:41:10 +0000</pubDate>
      <link>https://dev.to/suriyadisha/full-stack-ai-powered-email-generation-app-using-openai-api-part-1-596g</link>
      <guid>https://dev.to/suriyadisha/full-stack-ai-powered-email-generation-app-using-openai-api-part-1-596g</guid>
      <description>&lt;p&gt;Are you wondering 🤔 how to utilize the powerful API of ChatGPT 🤖 and make your own app?&lt;/p&gt;

&lt;p&gt;Watch this video 🎥 and learn to use the underlying API of ChatGPT with Node.js and React.js to build a simple yet powerful full stack ai email generation app.&lt;/p&gt;

&lt;p&gt;In this part of the series, you will learn:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;what is ChatGPT&lt;/li&gt;
&lt;li&gt;how to setup Node.js&lt;/li&gt;
&lt;li&gt;how to setup Express.js&lt;/li&gt;
&lt;li&gt;what is NPM&lt;/li&gt;
&lt;li&gt;how to initialize NPM&lt;/li&gt;
&lt;li&gt;how to install and implement packages like Nodemon, Body-parser, Cors, and Dotenv in the project&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/cxsx-187uzk"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Follow me and support me on:&lt;br&gt;
🔗 LinkedIn: &lt;a href="https://www.linkedin.com/in/suriya-ta"&gt;https://www.linkedin.com/in/suriya-ta&lt;/a&gt;...&lt;br&gt;
🐦 Twitter: &lt;a href="https://twitter.com/SuriyaDisha"&gt;https://twitter.com/SuriyaDisha&lt;/a&gt;&lt;br&gt;
📝 Tech Blog: &lt;a href="https://blog.suriyadisha.com/"&gt;https://blog.suriyadisha.com/&lt;/a&gt;&lt;br&gt;
☕ Buy me a coffee: &lt;a href="https://www.buymeacoffee.com/suriyadisha"&gt;https://www.buymeacoffee.com/suriyadisha&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>express</category>
      <category>openai</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
