<?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: HARSHITH GADDAM</title>
    <description>The latest articles on DEV Community by HARSHITH GADDAM (@harshith_gaddam).</description>
    <link>https://dev.to/harshith_gaddam</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%2F4039220%2F9859a55c-0f5a-4fe1-9c74-96e155044b54.jpg</url>
      <title>DEV Community: HARSHITH GADDAM</title>
      <link>https://dev.to/harshith_gaddam</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/harshith_gaddam"/>
    <language>en</language>
    <item>
      <title>TypeScript Basics, Jest Mocking, and Git Etiquette</title>
      <dc:creator>HARSHITH GADDAM</dc:creator>
      <pubDate>Thu, 27 Aug 2026 14:26:29 +0000</pubDate>
      <link>https://dev.to/harshith_gaddam/typescript-basics-jest-mocking-and-git-etiquette-eei</link>
      <guid>https://dev.to/harshith_gaddam/typescript-basics-jest-mocking-and-git-etiquette-eei</guid>
      <description>&lt;h1&gt;
  
  
  From Types to Testing: TypeScript Basics, Jest Mocking, and Git Etiquette
&lt;/h1&gt;

&lt;p&gt;When learning modern full-stack development, knowing a programming language is not enough. We also need to write clean code, test our code, and work properly with Git.&lt;/p&gt;

&lt;p&gt;In this post, we will look at some important basics of &lt;strong&gt;TypeScript, Jest testing, Git workflows, and code review etiquette&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. TypeScript Basics: Types, Interfaces &amp;amp; Generics
&lt;/h2&gt;

&lt;p&gt;TypeScript is JavaScript with &lt;strong&gt;static typing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It helps us find many errors before running our code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Types
&lt;/h3&gt;

&lt;p&gt;We can specify the type of a variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;developerName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Harshith&lt;/span&gt;&lt;span class="dl"&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;experienceYears&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&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;const&lt;/span&gt; &lt;span class="nx"&gt;isDeveloper&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;string&lt;/code&gt; → text&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;number&lt;/code&gt; → numbers&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;boolean&lt;/code&gt; → &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Interfaces
&lt;/h3&gt;

&lt;p&gt;An interface defines the structure of an object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;role&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&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;Now we can create a &lt;code&gt;User&lt;/code&gt; object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;currentUser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Harshith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;harshith@example.com&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;h3&gt;
  
  
  Important Interface Features
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;readonly&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value cannot be changed after the object is created.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Optional property&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;role&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;role&lt;/code&gt; property is optional.&lt;/p&gt;

&lt;p&gt;So this is also valid:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;102&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Rahul&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;rahul@example.com&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;h2&gt;
  
  
  2. Why TypeScript Is Useful
&lt;/h2&gt;

&lt;p&gt;JavaScript allows this:&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;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;twenty&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;p&gt;TypeScript can prevent this mistake:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Error&lt;/span&gt;
&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;twenty&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;p&gt;This makes large applications easier to maintain and debug.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Jest Testing Basics
&lt;/h2&gt;

&lt;p&gt;Jest is a JavaScript testing framework.&lt;/p&gt;

&lt;p&gt;It allows us to check whether our functions are working correctly.&lt;/p&gt;

&lt;p&gt;For 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;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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can test it using Jest:&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="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;adds two numbers&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;expect&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;2&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="nf"&gt;toBe&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;test()&lt;/code&gt; defines a test.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;expect()&lt;/code&gt; checks the result.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;toBe()&lt;/code&gt; compares the actual value with the expected value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Testing helps us find bugs before they reach production.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Jest Mocking
&lt;/h2&gt;

&lt;p&gt;Sometimes our function depends on another function or service.&lt;/p&gt;

&lt;p&gt;Instead of calling the real service during a test, we can use a &lt;strong&gt;mock&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For 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;const&lt;/span&gt; &lt;span class="nx"&gt;getUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mockReturnValue&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Harshith&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;p&gt;Now &lt;code&gt;getUser()&lt;/code&gt; returns our test data instead of calling a real database or API.&lt;/p&gt;

&lt;p&gt;Mocking is useful because tests become:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Faster&lt;/li&gt;
&lt;li&gt;More predictable&lt;/li&gt;
&lt;li&gt;Independent of external services&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  5. Git Feature Branches
&lt;/h2&gt;

&lt;p&gt;When working on a project, we should avoid making every change directly on the &lt;code&gt;main&lt;/code&gt; branch.&lt;/p&gt;

&lt;p&gt;Instead, we can create a feature branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git checkout &lt;span class="nt"&gt;-b&lt;/span&gt; feature/typescript-basics
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then make our changes and commit them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"feat: add TypeScript basics"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, push the branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push origin feature/typescript-basics
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can then create a Pull Request and ask other developers to review our changes.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. Code Review Etiquette
&lt;/h2&gt;

&lt;p&gt;Code reviews are not about finding mistakes in someone's code to criticize them.&lt;/p&gt;

&lt;p&gt;The main goal is to &lt;strong&gt;improve the code together&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"This code is bad."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Could we simplify this function to make it easier to maintain?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Good code review comments should be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clear&lt;/li&gt;
&lt;li&gt;Respectful&lt;/li&gt;
&lt;li&gt;Specific&lt;/li&gt;
&lt;li&gt;Helpful&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We should also explain &lt;strong&gt;why&lt;/strong&gt; we are suggesting a change.&lt;/p&gt;




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

&lt;p&gt;Today I learned that modern development involves more than just writing JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TypeScript&lt;/strong&gt; helps us write safer code with types and interfaces.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Jest&lt;/strong&gt; helps us test our code and find problems early.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Git branches&lt;/strong&gt; help us work safely without directly changing the main branch.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code review etiquette&lt;/strong&gt; helps teams communicate and improve code together.&lt;/p&gt;

&lt;p&gt;Learning these concepts will help me write cleaner code and work more effectively in a professional development environment.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>jest</category>
      <category>git</category>
    </item>
    <item>
      <title>My Javascript Learning Journey:Async,Await,iterators &amp; Generators</title>
      <dc:creator>HARSHITH GADDAM</dc:creator>
      <pubDate>Sat, 15 Aug 2026 13:03:36 +0000</pubDate>
      <link>https://dev.to/harshith_gaddam/my-javascript-learning-journeyasyncawaititerators-generators-2k56</link>
      <guid>https://dev.to/harshith_gaddam/my-javascript-learning-journeyasyncawaititerators-generators-2k56</guid>
      <description>&lt;p&gt;Today I learned several important JavaScript concepts related to asynchronous programming and iteration. Instead of only learning how these features work, I focused on understanding &lt;strong&gt;why they were introduced&lt;/strong&gt;, the problems they solve, and where they are used in real-world applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Async/Await – Syntax Sugar over Promises
&lt;/h2&gt;

&lt;p&gt;One of the biggest things I learned today is that &lt;strong&gt;&lt;code&gt;async/await&lt;/code&gt; does not replace Promises&lt;/strong&gt;. It is simply a cleaner and more readable way to work with them.&lt;/p&gt;

&lt;p&gt;When we write:&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUser&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&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;user&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;JavaScript is still using Promises internally. The &lt;code&gt;await&lt;/code&gt; keyword pauses the execution of the async function until the Promise settles, making asynchronous code look very similar to synchronous code.&lt;/p&gt;

&lt;p&gt;Without &lt;code&gt;async/await&lt;/code&gt;, the same logic would require multiple &lt;code&gt;.then()&lt;/code&gt; calls, making complex asynchronous code harder to read and maintain.&lt;/p&gt;




&lt;h2&gt;
  
  
  Handling Async Errors with try...catch
&lt;/h2&gt;

&lt;p&gt;When using Promises directly, errors are usually handled with &lt;code&gt;.catch()&lt;/code&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="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&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="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;async/await&lt;/code&gt;, the same error handling becomes much cleaner using &lt;code&gt;try...catch&lt;/code&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;loadUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&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;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&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;I learned that &lt;code&gt;try...catch&lt;/code&gt; only works with &lt;code&gt;await&lt;/code&gt; inside an &lt;code&gt;async&lt;/code&gt; function. If the awaited Promise rejects, JavaScript throws that rejection as an exception, which is then caught by the &lt;code&gt;catch&lt;/code&gt; block.&lt;/p&gt;

&lt;p&gt;This makes asynchronous error handling look just like normal synchronous error handling.&lt;/p&gt;




&lt;h2&gt;
  
  
  Promise.all()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Promise.all()&lt;/code&gt; is used when multiple asynchronous tasks need to finish before continuing.&lt;/p&gt;

&lt;p&gt;For example, imagine a dashboard that needs to load:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User Profile&lt;/li&gt;
&lt;li&gt;Orders&lt;/li&gt;
&lt;li&gt;Shopping Cart&lt;/li&gt;
&lt;li&gt;Recommendations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All these requests can start at the same time, but the dashboard should only be shown after every request is completed.&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="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nf"&gt;fetchProfile&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;fetchOrders&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;fetchCart&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="nf"&gt;fetchRecommendations&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;I also learned that &lt;code&gt;Promise.all()&lt;/code&gt; &lt;strong&gt;does not start&lt;/strong&gt; the promises. The promises begin executing when they are created (or when the function calls are evaluated). &lt;code&gt;Promise.all()&lt;/code&gt; simply waits for all of them to settle successfully.&lt;/p&gt;




&lt;h2&gt;
  
  
  Can We Display Data One by One?
&lt;/h2&gt;

&lt;p&gt;Yes!&lt;/p&gt;

&lt;p&gt;Instead of waiting for every request, each section of a webpage can be displayed as soon as its data arrives.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Show the profile first.&lt;/li&gt;
&lt;li&gt;Then show the orders.&lt;/li&gt;
&lt;li&gt;Then display the cart.&lt;/li&gt;
&lt;li&gt;Finally load the recommendations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Many websites like YouTube and Amazon use this approach to improve the user experience.&lt;/p&gt;




&lt;h2&gt;
  
  
  Promise.allSettled()
&lt;/h2&gt;

&lt;p&gt;Sometimes we don't want the whole operation to fail just because one task fails.&lt;/p&gt;

&lt;p&gt;For example, while uploading five files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;File 1 ✔️&lt;/li&gt;
&lt;li&gt;File 2 ❌&lt;/li&gt;
&lt;li&gt;File 3 ✔️&lt;/li&gt;
&lt;li&gt;File 4 ✔️&lt;/li&gt;
&lt;li&gt;File 5 ❌&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using &lt;code&gt;Promise.all()&lt;/code&gt;, the first failure rejects the entire operation.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;Promise.allSettled()&lt;/code&gt;, JavaScript waits for every promise to finish and tells us which ones succeeded and which ones failed.&lt;/p&gt;

&lt;p&gt;This is useful for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;File uploads&lt;/li&gt;
&lt;li&gt;Data synchronization&lt;/li&gt;
&lt;li&gt;Background jobs&lt;/li&gt;
&lt;li&gt;Health checks&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Promise.race()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Promise.race()&lt;/code&gt; returns the result of the first promise that settles.&lt;/p&gt;

&lt;p&gt;A common example is implementing request timeouts.&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;race&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/user&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;timeoutPromise&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the server responds first, we use the response.&lt;/p&gt;

&lt;p&gt;If the timeout finishes first, we stop waiting.&lt;/p&gt;




&lt;h2&gt;
  
  
  Promise.any()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Promise.any()&lt;/code&gt; returns the first &lt;strong&gt;successful&lt;/strong&gt; promise.&lt;/p&gt;

&lt;p&gt;Rejected promises are ignored unless &lt;strong&gt;every promise fails&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is useful when working with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple mirror servers&lt;/li&gt;
&lt;li&gt;Backup APIs&lt;/li&gt;
&lt;li&gt;CDN replicas&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The application simply uses the first successful response.&lt;/p&gt;




&lt;h2&gt;
  
  
  Promise Combinators Summary
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Promise.all()&lt;/td&gt;
&lt;td&gt;Wait for every promise to fulfill&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Promise.allSettled()&lt;/td&gt;
&lt;td&gt;Wait for every promise to settle, whether fulfilled or rejected&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Promise.race()&lt;/td&gt;
&lt;td&gt;Use the first promise that settles&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Promise.any()&lt;/td&gt;
&lt;td&gt;Use the first successful promise&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Iterators
&lt;/h2&gt;

&lt;p&gt;I learned that JavaScript needed a common way to loop through different types of collections like arrays, strings, maps, and sets.&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;iterator&lt;/strong&gt; is simply an object with a &lt;code&gt;next()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Each call to &lt;code&gt;next()&lt;/code&gt; returns:&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;done&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;for...of&lt;/code&gt; internally uses iterators to retrieve values one by one.&lt;/p&gt;




&lt;h2&gt;
  
  
  Generators
&lt;/h2&gt;

&lt;p&gt;Writing iterators manually requires managing state and keeping track of the current position.&lt;/p&gt;

&lt;p&gt;Generators make this much easier.&lt;/p&gt;

&lt;p&gt;A generator is declared using:&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="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="mi"&gt;3&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;The &lt;code&gt;yield&lt;/code&gt; keyword pauses execution and remembers the current state.&lt;/p&gt;

&lt;p&gt;Each call to &lt;code&gt;next()&lt;/code&gt; resumes execution from where it previously stopped.&lt;/p&gt;

&lt;p&gt;Generators automatically create iterators, making iteration much simpler.&lt;/p&gt;




&lt;h2&gt;
  
  
  Async Iterators
&lt;/h2&gt;

&lt;p&gt;Normal iterators work only with values that are immediately available.&lt;/p&gt;

&lt;p&gt;But what if the next value comes from:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A server&lt;/li&gt;
&lt;li&gt;A database&lt;/li&gt;
&lt;li&gt;A file&lt;/li&gt;
&lt;li&gt;A WebSocket&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In these situations, JavaScript introduced &lt;strong&gt;async iterators&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Unlike normal iterators, &lt;code&gt;next()&lt;/code&gt; returns a Promise that eventually resolves to:&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;done&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows JavaScript to wait until the next value becomes available.&lt;/p&gt;




&lt;h2&gt;
  
  
  Async Generators
&lt;/h2&gt;

&lt;p&gt;Just as generators simplify iterators, async generators simplify async iterators.&lt;/p&gt;

&lt;p&gt;They are declared like this:&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;First&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Second&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Third&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;They can be consumed using:&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="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;await &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;item&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nf"&gt;stream&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;item&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;This makes working with asynchronous streams much easier.&lt;/p&gt;




&lt;h2&gt;
  
  
  Where Async Iterators Are Used
&lt;/h2&gt;

&lt;p&gt;Some real-world examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Streaming large files&lt;/li&gt;
&lt;li&gt;Reading database records in batches&lt;/li&gt;
&lt;li&gt;Live chat messages&lt;/li&gt;
&lt;li&gt;WebSocket communication&lt;/li&gt;
&lt;li&gt;AI response streaming (like ChatGPT)&lt;/li&gt;
&lt;li&gt;Video or audio streaming&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of loading everything into memory, data is processed one piece at a time.&lt;/p&gt;




&lt;h1&gt;
  
  
  My Biggest Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;async/await&lt;/code&gt; is syntax sugar over Promises and makes asynchronous code easier to read.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;try...catch&lt;/code&gt; provides clean error handling for asynchronous code written with &lt;code&gt;async/await&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Promise.all()&lt;/code&gt; waits for every promise and fails if any one fails.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Promise.allSettled()&lt;/code&gt; gives the result of every promise, whether it succeeds or fails.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Promise.race()&lt;/code&gt; returns whichever promise settles first.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Promise.any()&lt;/code&gt; returns the first successful promise.&lt;/li&gt;
&lt;li&gt;Promises begin running when they are created; &lt;code&gt;Promise.all()&lt;/code&gt; only waits for them.&lt;/li&gt;
&lt;li&gt;A webpage doesn't always need to wait for every API before showing content—independent sections can be rendered progressively.&lt;/li&gt;
&lt;li&gt;Iterators provide a standard way to access values one by one.&lt;/li&gt;
&lt;li&gt;Generators automatically create iterators and use &lt;code&gt;yield&lt;/code&gt; to pause and resume execution.&lt;/li&gt;
&lt;li&gt;Async iterators return promises from &lt;code&gt;next()&lt;/code&gt;, allowing values to arrive over time.&lt;/li&gt;
&lt;li&gt;Async generators combine &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;yield&lt;/code&gt;, making asynchronous data streams much easier to work with.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>learning</category>
    </item>
    <item>
      <title>My JavaScript Learning Journey: CommonJS vs ESM,Design Patterns</title>
      <dc:creator>HARSHITH GADDAM</dc:creator>
      <pubDate>Fri, 14 Aug 2026 10:24:42 +0000</pubDate>
      <link>https://dev.to/harshith_gaddam/my-javascript-learning-journey-commonjs-vs-esmdesign-patterns-i3j</link>
      <guid>https://dev.to/harshith_gaddam/my-javascript-learning-journey-commonjs-vs-esmdesign-patterns-i3j</guid>
      <description>&lt;h1&gt;
  
  
  JavaScript Architecture, Design Patterns, Memory Mechanics.
&lt;/h1&gt;

&lt;p&gt;Today I learned about &lt;strong&gt;core JavaScript architecture, modules, design patterns, memory mechanics&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Understanding how JavaScript handles files, memory, and software design makes it much easier to write clean, scalable, and maintainable applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. CommonJS (CJS) vs. ES Modules (ESM)
&lt;/h2&gt;

&lt;p&gt;JavaScript uses modules to split code across multiple files and avoid polluting the global scope.&lt;/p&gt;

&lt;h3&gt;
  
  
  CommonJS (CJS)
&lt;/h3&gt;

&lt;p&gt;CommonJS is the traditional module system used heavily in Node.js.&lt;/p&gt;

&lt;p&gt;It uses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;require()&lt;/code&gt; for importing&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;module.exports&lt;/code&gt; for exporting
&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="c1"&gt;// greet.js&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&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="nx"&gt;name&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="s2"&gt;`Hello, &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="s2"&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;Importing it:&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;const&lt;/span&gt; &lt;span class="nx"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./greet.js&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="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Harshith&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;p&gt;CommonJS module loading is generally synchronous.&lt;/p&gt;

&lt;h3&gt;
  
  
  ES Modules (ESM)
&lt;/h3&gt;

&lt;p&gt;ES Modules are the modern JavaScript module standard and are supported by both browsers and modern Node.js.&lt;/p&gt;

&lt;p&gt;They use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;export&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;export default&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;import&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="c1"&gt;// greet.js&lt;/span&gt;
&lt;span class="k"&gt;export&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="nx"&gt;name&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="s2"&gt;`Hello, &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="s2"&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;Importing:&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="k"&gt;import&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;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./greet.js&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="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Harshith&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;p&gt;ESM uses static module declarations, allowing JavaScript engines and tooling to analyze module dependencies before execution.&lt;/p&gt;

&lt;h3&gt;
  
  
  Named Export vs. Default Export
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Named Export&lt;/th&gt;
&lt;th&gt;Default Export&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Number per file&lt;/td&gt;
&lt;td&gt;Multiple&lt;/td&gt;
&lt;td&gt;One&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Import syntax&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;{}&lt;/code&gt; required&lt;/td&gt;
&lt;td&gt;No &lt;code&gt;{}&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Import name&lt;/td&gt;
&lt;td&gt;Must match exported name&lt;/td&gt;
&lt;td&gt;Can be any name&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Example&lt;/td&gt;
&lt;td&gt;&lt;code&gt;import { add } from "./math.js"&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;`import&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Calculator from "./Calculator.js"` |&lt;/p&gt;

&lt;p&gt;For 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="c1"&gt;// Named exports&lt;/span&gt;
&lt;span class="k"&gt;export&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;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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;subtract&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Importing:&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;subtract&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./math.js&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;p&gt;A default export:&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;User&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can be imported as:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./User.js&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;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Named imports use curly braces because they refer to specific named exports. They are not simply the same thing as ordinary object destructuring.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  2. Garbage Collection, Closures, and Memory Leaks
&lt;/h2&gt;

&lt;p&gt;JavaScript automatically manages memory using &lt;strong&gt;garbage collection&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Garbage Collection
&lt;/h3&gt;

&lt;p&gt;A common model used by JavaScript engines is &lt;strong&gt;Mark-and-Sweep&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The engine starts from reachable roots, such as global references, and follows references to determine which objects are still reachable.&lt;/p&gt;

&lt;p&gt;Objects that are no longer reachable can eventually be removed from memory.&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Harshith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After assigning &lt;code&gt;null&lt;/code&gt;, the object previously referenced by &lt;code&gt;user&lt;/code&gt; may become unreachable and eligible for garbage collection, assuming nothing else references it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Closures
&lt;/h3&gt;

&lt;p&gt;A closure occurs when an inner function retains access to variables from its outer lexical scope even after the outer function has finished executing.&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;createCounter&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;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&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;count&lt;/span&gt;&lt;span class="p"&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;const&lt;/span&gt; &lt;span class="nx"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createCounter&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="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 1&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="nf"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though &lt;code&gt;createCounter()&lt;/code&gt; has finished executing, the returned function still has access to &lt;code&gt;count&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Memory Leaks
&lt;/h3&gt;

&lt;p&gt;A memory leak occurs when data that is no longer logically needed remains reachable, preventing garbage collection.&lt;/p&gt;

&lt;p&gt;Common causes include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uncleared timers&lt;/li&gt;
&lt;li&gt;Event listeners that are never removed&lt;/li&gt;
&lt;li&gt;Long-lived references to large objects&lt;/li&gt;
&lt;li&gt;Closures that unintentionally retain large data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For 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;setupListener&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;heavyData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;fill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;📦&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&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="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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Clicked!&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important lesson is that &lt;strong&gt;closures themselves are not memory leaks&lt;/strong&gt;. A closure becomes a problem when it keeps references alive longer than necessary.&lt;/p&gt;




&lt;h1&gt;
  
  
  3. Four Essential Design Patterns
&lt;/h1&gt;

&lt;p&gt;Design patterns are reusable approaches for solving common software design problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  A. Module Pattern
&lt;/h2&gt;

&lt;p&gt;The Module Pattern encapsulates internal state and exposes only the functionality that should be public.&lt;/p&gt;

&lt;p&gt;A closure can be used to create private state:&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;const&lt;/span&gt; &lt;span class="nx"&gt;BankModule&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;function &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;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nx"&gt;amount&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;balance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&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;Here, &lt;code&gt;balance&lt;/code&gt; cannot be accessed directly from outside the module.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy:&lt;/strong&gt; An ATM hides the internal banking system while exposing only operations such as withdraw and check balance.&lt;/p&gt;




&lt;h2&gt;
  
  
  B. Singleton Pattern
&lt;/h2&gt;

&lt;p&gt;The Singleton Pattern ensures that only one instance of a particular object is used.&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;class&lt;/span&gt; &lt;span class="nc"&gt;DatabaseConnection&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

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

        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;DatabaseConnection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;instance&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;DatabaseConnection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connectionString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mongodb://localhost:27017&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="nx"&gt;DatabaseConnection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&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;const&lt;/span&gt; &lt;span class="nx"&gt;db1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DatabaseConnection&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;db2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DatabaseConnection&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;db1&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;db2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both variables refer to the same instance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy:&lt;/strong&gt; Think of a single shared resource that the application should coordinate through one instance.&lt;/p&gt;




&lt;h2&gt;
  
  
  C. Factory Pattern
&lt;/h2&gt;

&lt;p&gt;The Factory Pattern centralizes object creation.&lt;/p&gt;

&lt;p&gt;Instead of spreading object-creation logic throughout the application, a factory decides which object to create.&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;class&lt;/span&gt; &lt;span class="nc"&gt;UserFactory&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;type&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="p"&gt;{&lt;/span&gt;

        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;admin&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="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&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="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;READ&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;WRITE&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;guest&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="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&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="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;READ&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="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;admin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;UserFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;admin&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Sarah&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;p&gt;The caller doesn't need to know all the details required to create each type of user.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analogy:&lt;/strong&gt; A fast-food kitchen receives your order and handles the details of assembling the meal.&lt;/p&gt;




&lt;h2&gt;
  
  
  D. Observer Pattern
&lt;/h2&gt;

&lt;p&gt;The Observer Pattern creates a one-to-many relationship where subscribers are notified when an event occurs.&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;class&lt;/span&gt; &lt;span class="nc"&gt;EventEmitter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt; &lt;span class="o"&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;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;listener&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="p"&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;Multiple listeners can subscribe to an event:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Event occurs
     ↓
EventEmitter
     ↓
 ┌───┼───┐
 ↓   ↓   ↓
L1  L2  L3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helps create loosely coupled systems.&lt;/p&gt;




&lt;h1&gt;
  
  
  4. Functional Strategy Pattern
&lt;/h1&gt;

&lt;p&gt;The Strategy Pattern allows us to choose between different algorithms without changing the main logic that uses them.&lt;/p&gt;

&lt;p&gt;In functional JavaScript, strategies can simply be functions.&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;const&lt;/span&gt; &lt;span class="nx"&gt;fedexStrategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pkg&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;pkg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;weight&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;1.5&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="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;uspsStrategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pkg&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;pkg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;weight&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.9&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&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;We can create a function that accepts a strategy:&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;const&lt;/span&gt; &lt;span class="nx"&gt;createShippingCalculator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;strategy&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;pkg&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="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pkg&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;Then create a pre-configured calculator:&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;const&lt;/span&gt; &lt;span class="nx"&gt;calculateFedex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nf"&gt;createShippingCalculator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fedexStrategy&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="nf"&gt;calculateFedex&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&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;The important idea is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Shipping Calculator
       ↓
   Strategy
   ↙     ↘
FedEx    USPS
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can change the strategy without changing the calculator itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding &lt;code&gt;(strategy) =&amp;gt; (pkg) =&amp;gt; ...&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This syntax means that the outer function returns another function.&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;const&lt;/span&gt; &lt;span class="nx"&gt;createShippingCalculator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;strategy&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;pkg&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="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pkg&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;Conceptually:&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;const&lt;/span&gt; &lt;span class="nx"&gt;createShippingCalculator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;strategy&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pkg&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="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pkg&lt;/span&gt;&lt;span class="p"&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;The returned function remembers &lt;code&gt;strategy&lt;/code&gt; through a &lt;strong&gt;closure&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. The SOLID Principles
&lt;/h1&gt;

&lt;p&gt;SOLID is a collection of five principles that help us design maintainable and extensible software.&lt;/p&gt;

&lt;h2&gt;
  
  
  S — Single Responsibility Principle (SRP)
&lt;/h2&gt;

&lt;p&gt;A class or function should have &lt;strong&gt;one clear responsibility&lt;/strong&gt; and one main reason to change.&lt;/p&gt;

&lt;p&gt;Instead of:&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;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;saveToDatabase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="nf"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="nf"&gt;generateReport&lt;/span&gt;&lt;span class="p"&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;we can separate responsibilities:&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;class&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Save user&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nf"&gt;sendWelcomeEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Send email&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;Each class focuses on a specific responsibility.&lt;/p&gt;




&lt;h2&gt;
  
  
  O — Open/Closed Principle (OCP)
&lt;/h2&gt;

&lt;p&gt;Software should be &lt;strong&gt;open for extension but closed for modification&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For 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;class&lt;/span&gt; &lt;span class="nc"&gt;CreditCardPayment&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&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="s2"&gt;`Paid $&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; with Credit Card`&lt;/span&gt;&lt;span class="p"&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;class&lt;/span&gt; &lt;span class="nc"&gt;PayPalPayment&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&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="s2"&gt;`Paid $&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; with PayPal`&lt;/span&gt;&lt;span class="p"&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;class&lt;/span&gt; &lt;span class="nc"&gt;PaymentProcessor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;paymentStrategy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;paymentStrategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amount&lt;/span&gt;&lt;span class="p"&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;We can add another payment strategy without modifying &lt;code&gt;PaymentProcessor&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PaymentProcessor
       ↓
   Strategy
   ↙  ↓  ↘
Card PayPal UPI
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  L — Liskov Substitution Principle (LSP)
&lt;/h2&gt;

&lt;p&gt;A subclass should be usable wherever its parent type is expected without breaking the application's behavior.&lt;/p&gt;

&lt;p&gt;A problematic design would be:&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;class&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;fly&lt;/span&gt;&lt;span class="p"&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;class&lt;/span&gt; &lt;span class="nc"&gt;Ostrich&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;fly&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Cannot fly&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The parent class promises that birds can fly, but &lt;code&gt;Ostrich&lt;/code&gt; cannot satisfy that expectation.&lt;/p&gt;

&lt;p&gt;A better hierarchy is:&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;class&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FlyingBird&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nf"&gt;fly&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Flying&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;FlyingBird&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Ostrich&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;Ostrich&lt;/code&gt; doesn't inherit behavior it cannot support.&lt;/p&gt;




&lt;h2&gt;
  
  
  I — Interface Segregation Principle (ISP)
&lt;/h2&gt;

&lt;p&gt;Clients should not be forced to depend on methods they don't need.&lt;/p&gt;

&lt;p&gt;The general idea is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Prefer small, focused interfaces or contracts rather than one large interface containing everything.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;JavaScript doesn't have traditional interfaces like some languages, but the principle still applies when designing classes, objects, and APIs.&lt;/p&gt;




&lt;h2&gt;
  
  
  D — Dependency Inversion Principle (DIP)
&lt;/h2&gt;

&lt;p&gt;High-level modules should not be tightly coupled to specific low-level implementations.&lt;/p&gt;

&lt;p&gt;Instead, dependencies can be provided from outside.&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;class&lt;/span&gt; &lt;span class="nc"&gt;OrderProcessor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;order&lt;/span&gt;&lt;span class="p"&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;Here, &lt;code&gt;OrderProcessor&lt;/code&gt; doesn't create the database itself.&lt;/p&gt;

&lt;p&gt;The dependency is &lt;strong&gt;injected&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;const&lt;/span&gt; &lt;span class="nx"&gt;database&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Database&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;processor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OrderProcessor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes the code easier to test and replace with another implementation.&lt;/p&gt;




&lt;h1&gt;
  
  
  javascript #webdevelopment #programming #softwareengineering #designpatterns #solidprinciples #nodejs #learninginpublic
&lt;/h1&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>My JavaScript Learning Journey: Call Back Hell &amp; Promises</title>
      <dc:creator>HARSHITH GADDAM</dc:creator>
      <pubDate>Tue, 04 Aug 2026 11:54:33 +0000</pubDate>
      <link>https://dev.to/harshith_gaddam/my-javascript-learning-journey-call-back-hell-promises-56b</link>
      <guid>https://dev.to/harshith_gaddam/my-javascript-learning-journey-call-back-hell-promises-56b</guid>
      <description>&lt;p&gt;Today I learned one of the most important concepts in JavaScript—&lt;strong&gt;asynchronous programming with Promises&lt;/strong&gt;. Initially, Promises seemed confusing, but after understanding how they work internally, the concept became much clearer.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What is Callback Hell?
&lt;/h2&gt;

&lt;p&gt;A callback is a function passed to another function that executes after an asynchronous operation completes.&lt;/p&gt;

&lt;p&gt;When multiple asynchronous operations depend on each other, callbacks become deeply nested.&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="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;getOrders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;getOrderDetails&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;details&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;processOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;details&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&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="p"&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;This deeply nested structure is called &lt;strong&gt;Callback Hell&lt;/strong&gt; or the &lt;strong&gt;Pyramid of Doom&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problems with Callback Hell
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Difficult to read&lt;/li&gt;
&lt;li&gt;Hard to debug&lt;/li&gt;
&lt;li&gt;Difficult to maintain&lt;/li&gt;
&lt;li&gt;Error handling becomes complicated&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To solve these problems, JavaScript introduced &lt;strong&gt;Promises&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  2. What is a Promise?
&lt;/h1&gt;

&lt;p&gt;A &lt;strong&gt;Promise&lt;/strong&gt; is an object that represents the eventual result of an asynchronous operation.&lt;/p&gt;

&lt;p&gt;It promises that it will either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Complete successfully&lt;/li&gt;
&lt;li&gt;Fail with an error&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A Promise starts in the &lt;strong&gt;Pending&lt;/strong&gt; state and eventually becomes either &lt;strong&gt;Fulfilled&lt;/strong&gt; or &lt;strong&gt;Rejected&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  3. Promise States
&lt;/h1&gt;

&lt;p&gt;Every Promise has three possible states.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pending
&lt;/h3&gt;

&lt;p&gt;The asynchronous operation is still running.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fulfilled
&lt;/h3&gt;

&lt;p&gt;The operation completed successfully.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rejected
&lt;/h3&gt;

&lt;p&gt;The operation failed.&lt;/p&gt;

&lt;p&gt;A Promise can change its state only once.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Pending
   |
   | resolve()
   ▼
Fulfilled

OR

Pending
   |
   | reject()
   ▼
Rejected
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  4. Creating a Promise
&lt;/h1&gt;

&lt;p&gt;A Promise is created using the &lt;code&gt;Promise&lt;/code&gt; constructor.&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;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One important thing I learned today is that &lt;strong&gt;I do not create &lt;code&gt;resolve&lt;/code&gt; and &lt;code&gt;reject&lt;/code&gt; myself.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;JavaScript automatically provides these two functions to the Promise executor.&lt;/p&gt;

&lt;p&gt;Conceptually, it works like this:&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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;resolve()&lt;/code&gt; is used when the operation succeeds.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;reject()&lt;/code&gt; is used when the operation fails.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  5. Using resolve() and reject()
&lt;/h1&gt;

&lt;p&gt;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;const&lt;/span&gt; &lt;span class="nx"&gt;promise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;marks&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pass&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Fail&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;If the condition is true, the Promise becomes &lt;strong&gt;Fulfilled&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Otherwise, it becomes &lt;strong&gt;Rejected&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  6. Handling Promises
&lt;/h1&gt;

&lt;h3&gt;
  
  
  then()
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;.then()&lt;/code&gt; executes only when the Promise is fulfilled.&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;promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  catch()
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;.catch()&lt;/code&gt; executes only when the Promise is rejected.&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;promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;error&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;h3&gt;
  
  
  finally()
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;.finally()&lt;/code&gt; executes whether the Promise succeeds or fails.&lt;/p&gt;

&lt;p&gt;It is useful for cleanup operations like hiding loading indicators or closing connections.&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;promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;finally&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Finished&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;h1&gt;
  
  
  7. Promise Chaining
&lt;/h1&gt;

&lt;p&gt;Every &lt;code&gt;.then()&lt;/code&gt;, &lt;code&gt;.catch()&lt;/code&gt;, and &lt;code&gt;.finally()&lt;/code&gt; returns another Promise.&lt;/p&gt;

&lt;p&gt;This allows us to chain multiple asynchronous operations.&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;value&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;value&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;25
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each &lt;code&gt;.then()&lt;/code&gt; receives the value returned by the previous &lt;code&gt;.then()&lt;/code&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  8. Error Propagation
&lt;/h1&gt;

&lt;p&gt;One of the most interesting concepts I learned today is &lt;strong&gt;Error Propagation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If an error occurs inside any &lt;code&gt;.then()&lt;/code&gt; callback, JavaScript automatically skips all remaining &lt;code&gt;.then()&lt;/code&gt; callbacks and jumps directly to the nearest &lt;code&gt;.catch()&lt;/code&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Something went wrong&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This will never execute&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="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;error&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Something went wrong
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes error handling much simpler because a single &lt;code&gt;.catch()&lt;/code&gt; can handle errors from multiple Promise operations.&lt;/p&gt;




&lt;h1&gt;
  
  
  9. Practical Promise Example
&lt;/h1&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;login&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;span class="nx"&gt;password&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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&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;harshith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1234&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;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Login Successful&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Invalid Username or Password&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="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&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;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;harshith&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1234&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;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&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="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="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example helped me understand when to call &lt;code&gt;resolve()&lt;/code&gt; and &lt;code&gt;reject()&lt;/code&gt; based on a condition.&lt;/p&gt;




&lt;h1&gt;
  
  
  10. Key Takeaways
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;Callback Hell makes asynchronous code difficult to read and maintain.&lt;/li&gt;
&lt;li&gt;Promises solve Callback Hell by making asynchronous code cleaner.&lt;/li&gt;
&lt;li&gt;Every Promise has three states: &lt;strong&gt;Pending&lt;/strong&gt;, &lt;strong&gt;Fulfilled&lt;/strong&gt;, and &lt;strong&gt;Rejected&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;JavaScript automatically provides the &lt;code&gt;resolve()&lt;/code&gt; and &lt;code&gt;reject()&lt;/code&gt; functions when creating a Promise.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;resolve()&lt;/code&gt; changes a Promise to &lt;strong&gt;Fulfilled&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;reject()&lt;/code&gt; changes a Promise to &lt;strong&gt;Rejected&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.then()&lt;/code&gt; handles successful results.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.catch()&lt;/code&gt; handles errors and rejected Promises.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.finally()&lt;/code&gt; executes regardless of success or failure.&lt;/li&gt;
&lt;li&gt;Promise chaining makes complex asynchronous operations easier to manage.&lt;/li&gt;
&lt;li&gt;Errors automatically propagate through the Promise chain until they are handled by the nearest &lt;code&gt;.catch()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




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

&lt;p&gt;Today's learning helped me understand how JavaScript manages asynchronous operations using Promises.I learned why callback hell occurs, how Promises improve code readability, how Promise states work, how &lt;code&gt;resolve()&lt;/code&gt; and &lt;code&gt;reject()&lt;/code&gt; are provided by JavaScript, and how &lt;code&gt;.then()&lt;/code&gt;, &lt;code&gt;.catch()&lt;/code&gt;, &lt;code&gt;.finally()&lt;/code&gt;, Promise chaining, and error propagation make asynchronous programming much cleaner and easier to maintain.&lt;/p&gt;

&lt;p&gt;Understanding these concepts has also given me a much stronger foundation for learning &lt;strong&gt;&lt;code&gt;async/await&lt;/code&gt;&lt;/strong&gt;, since it is built on top of Promises.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>blog</category>
      <category>learning</category>
    </item>
    <item>
      <title>My JavaScript Learning Journey:Object-Oriented Programming (OOP) in JavaScript</title>
      <dc:creator>HARSHITH GADDAM</dc:creator>
      <pubDate>Mon, 27 Jul 2026 17:08:59 +0000</pubDate>
      <link>https://dev.to/harshith_gaddam/my-javascript-learning-journey4-pillars-of-oops-350</link>
      <guid>https://dev.to/harshith_gaddam/my-javascript-learning-journey4-pillars-of-oops-350</guid>
      <description>&lt;p&gt;JavaScript supports Object-Oriented Programming (OOP) through objects, prototypes, and classes. ES6 introduced a cleaner class syntax, making OOP easier to understand and write. In this blog, I'll explain the four pillars of OOP and some useful class features in JavaScript.&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Inheritance
&lt;/h1&gt;

&lt;p&gt;Inheritance allows one class to reuse the properties and methods of another class.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;class Animal {&lt;br&gt;
    eat() {&lt;br&gt;
        console.log("Eating");&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;class Dog extends Animal {}&lt;/p&gt;

&lt;p&gt;const dog = new Dog();&lt;/p&gt;

&lt;p&gt;dog.eat();&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;Dog&lt;/code&gt; extends &lt;code&gt;Animal&lt;/code&gt;, it inherits the &lt;code&gt;eat()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;This helps us avoid writing the same code again.&lt;/p&gt;


&lt;h1&gt;
  
  
  2. Polymorphism
&lt;/h1&gt;

&lt;p&gt;Polymorphism means &lt;strong&gt;one method can behave differently depending on the object that calls it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In JavaScript, this is usually achieved by overriding a parent class method in a child class.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;class Animal {&lt;br&gt;
    speak() {&lt;br&gt;
        console.log("Animal makes a sound");&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;class Dog extends Animal {&lt;br&gt;
    speak() {&lt;br&gt;
        console.log("Dog barks");&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;class Cat extends Animal {&lt;br&gt;
    speak() {&lt;br&gt;
        console.log("Cat meows");&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;const dog = new Dog();&lt;br&gt;
const cat = new Cat();&lt;/p&gt;

&lt;p&gt;dog.speak();&lt;br&gt;
cat.speak();&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;Dog barks&lt;br&gt;
Cat meows&lt;/p&gt;

&lt;p&gt;Here, all classes have the same &lt;code&gt;speak()&lt;/code&gt; method, but each class provides its own implementation. This is called &lt;strong&gt;polymorphism&lt;/strong&gt;.&lt;/p&gt;


&lt;h1&gt;
  
  
  3. Abstraction
&lt;/h1&gt;

&lt;p&gt;Abstraction means &lt;strong&gt;showing only the necessary functionality and hiding the internal implementation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, when we start a car, we only press the start button. We don't need to know how the engine, fuel system, and battery work internally.&lt;/p&gt;

&lt;p&gt;JavaScript allows us to achieve abstraction using classes and private methods or private fields.&lt;/p&gt;
&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;class Car {&lt;br&gt;
    start() {&lt;br&gt;
        this.#checkFuel();&lt;br&gt;
        console.log("Car Started");&lt;br&gt;
    }&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#checkFuel() {
    console.log("Checking Fuel...");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;const car = new Car();&lt;/p&gt;

&lt;p&gt;car.start();&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Checking Fuel...
Car Started
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The user only calls &lt;code&gt;start()&lt;/code&gt;. The internal method &lt;code&gt;#checkFuel()&lt;/code&gt; remains hidden from outside the class. This makes the code easier to use and protects the internal implementation.&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Static Members
&lt;/h1&gt;

&lt;p&gt;Normally, we create an object before calling a method.&lt;/p&gt;

&lt;p&gt;const obj = new Person();&lt;/p&gt;

&lt;p&gt;But some methods belong to the class itself, not to its objects.&lt;/p&gt;

&lt;p&gt;These are called &lt;strong&gt;static methods&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;class MathUtil {&lt;br&gt;
    static add(a, b) {&lt;br&gt;
        return a + b;&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;console.log(MathUtil.add(5, 3));&lt;/p&gt;

&lt;p&gt;Notice that we didn't create an object.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. Getters and Setters
&lt;/h1&gt;

&lt;p&gt;Getters and setters allow us to control how properties are read and updated.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;class Person {&lt;br&gt;
    constructor(name) {&lt;br&gt;
        this._name = name;&lt;br&gt;
    }&lt;br&gt;
    get name() {&lt;br&gt;
        return this._name;&lt;br&gt;
    }&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;set name(value) {
    if (value.length &amp;gt;= 3) {
        this._name = value;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;This helps us validate data before updating it.&lt;/p&gt;




&lt;h1&gt;
  
  
  6. Encapsulation Using Closures
&lt;/h1&gt;

&lt;p&gt;Encapsulation means bundling data and methods into a single unit.And Closure means accessing the outer methods variables inside inner methods after outer function(method) has completed its execution.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;function BankAccount() {&lt;br&gt;
    let balance = 1000;&lt;br&gt;
    return {&lt;br&gt;
        deposit(amount) {&lt;br&gt;
            balance += amount;&lt;br&gt;
        },&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    getBalance() {
        return balance;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
const account = BankAccount();&lt;br&gt;
account.deposit(500);&lt;br&gt;
console.log(account.getBalance());&lt;br&gt;
//console.log(account.balance); will throw an error&lt;/p&gt;

&lt;p&gt;The variable &lt;code&gt;balance&lt;/code&gt; cannot be accessed directly from outside the function.&lt;/p&gt;

&lt;p&gt;In the above Example you can see balance,deposit(),getBalance() are bundled together.&lt;/p&gt;




&lt;h1&gt;
  
  
  7. Private Fields (&lt;code&gt;#field&lt;/code&gt;)
&lt;/h1&gt;

&lt;p&gt;Modern JavaScript provides another way to hide data using private fields.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;class BankAccount {&lt;br&gt;
    #balance = 1000;&lt;br&gt;
    deposit(amount) {&lt;br&gt;
        this.#balance += amount;&lt;br&gt;
    }&lt;br&gt;
    getBalance() {&lt;br&gt;
        return this.#balance;&lt;br&gt;
    }&lt;br&gt;
}&lt;br&gt;
const account = new BankAccount();&lt;br&gt;
account.deposit(500);&lt;br&gt;
console.log(account.getBalance());&lt;br&gt;
Trying to access:&lt;br&gt;
account.#balance;&lt;/p&gt;

&lt;p&gt;will cause an error because private fields can only be accessed inside the class.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Now I know that JavaScript's OOP system is built on prototypes, and classes simply make the syntax cleaner and easier to read. Understanding these concepts gives a strong foundation for writing better JavaScript code.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>My JavaScript Learning Journey:Prototypes</title>
      <dc:creator>HARSHITH GADDAM</dc:creator>
      <pubDate>Mon, 27 Jul 2026 12:17:05 +0000</pubDate>
      <link>https://dev.to/harshith_gaddam/my-javascript-learning-journeyprototypes-and-object-oriented-programming-oop-2765</link>
      <guid>https://dev.to/harshith_gaddam/my-javascript-learning-journeyprototypes-and-object-oriented-programming-oop-2765</guid>
      <description>&lt;p&gt;JavaScript is different from many other programming languages when it comes to Object-Oriented Programming (OOP) because it is prototype-based. Instead of using classes as its foundation, JavaScript uses prototypes. &lt;/p&gt;

&lt;p&gt;In this blog, I'll explain the topics I learned today.&lt;/p&gt;

&lt;h1&gt;
  
  
  1. What is a Prototype?
&lt;/h1&gt;

&lt;p&gt;A &lt;strong&gt;prototype&lt;/strong&gt; is a shared object that stores common methods and properties.&lt;/p&gt;

&lt;p&gt;Instead of creating a separate copy of the same method for every object, JavaScript stores it once in the prototype, and all objects created from the same constructor or class share it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;function Person(name) {&lt;br&gt;
    this.name = name;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Person.prototype.greet = function () {&lt;br&gt;
    console.log(&lt;code&gt;Hello, I'm ${this.name}&lt;/code&gt;);&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;const p1 = new Person("Harshith");&lt;br&gt;
const p2 = new Person("Rahul");&lt;/p&gt;

&lt;p&gt;p1.greet();&lt;br&gt;
p2.greet();&lt;/p&gt;

&lt;p&gt;Here, both &lt;code&gt;p1&lt;/code&gt; and &lt;code&gt;p2&lt;/code&gt; use the same &lt;code&gt;greet()&lt;/code&gt; method from &lt;code&gt;Person.prototype&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This saves memory because JavaScript doesn't create multiple copies of the same function.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Prototype vs &lt;code&gt;__proto__&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;This topic confused me at first, but here's the difference.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;prototype&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Exists on &lt;strong&gt;functions&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Used when creating new objects with the &lt;code&gt;new&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;Stores shared methods.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;function Person(name) {&lt;br&gt;
    this.name = name;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Person.prototype.greet = function () {};&lt;br&gt;
const p1 = new Person("Harshith");&lt;br&gt;
 p1 will have a property called "&lt;strong&gt;proto&lt;/strong&gt;" there it stores the prototype of Person&lt;br&gt;
p1.&lt;strong&gt;proto&lt;/strong&gt;=Person.prototype;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;__proto__&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Exists on &lt;strong&gt;objects&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Points to the object's prototype.&lt;/li&gt;
&lt;li&gt;JavaScript follows this link while searching for properties.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;const person = new Person("Harshith");&lt;/p&gt;

&lt;p&gt;console.log(person.&lt;strong&gt;proto&lt;/strong&gt; === Person.prototype);&lt;/p&gt;

&lt;p&gt;A simple way to remember:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;prototype&lt;/code&gt; belongs to functions.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;__proto__&lt;/code&gt; belongs to objects.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  3. Prototype Chain
&lt;/h1&gt;

&lt;p&gt;When JavaScript cannot find a property inside an object, it searches its prototype.&lt;/p&gt;

&lt;p&gt;If it is still not found, it continues searching through the next prototype until it reaches &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;const animal = {&lt;br&gt;
    eat() {&lt;br&gt;
        console.log("Eating");&lt;br&gt;
    }&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;const dog = Object.create(animal);&lt;/p&gt;

&lt;p&gt;dog.eat();&lt;/p&gt;

&lt;p&gt;JavaScript searches like this:&lt;/p&gt;

&lt;p&gt;dog&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;animal.prototype&lt;/p&gt;

&lt;p&gt;↓&lt;/p&gt;

&lt;p&gt;null&lt;/p&gt;

&lt;p&gt;This searching process is called the &lt;strong&gt;Prototype Chain&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  4. Object.create()
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;Object.create()&lt;/code&gt; creates a new object whose prototype is another object.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;const animal = {&lt;br&gt;
    eat() {&lt;br&gt;
        console.log("Eating");&lt;br&gt;
    }&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;const dog = Object.create(animal);&lt;/p&gt;

&lt;p&gt;dog.eat();&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;dog&lt;/code&gt; object doesn't have its own &lt;code&gt;eat()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;It inherits it from &lt;code&gt;animal&lt;/code&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  5. Classes are Syntactic Sugar
&lt;/h1&gt;

&lt;p&gt;Before ES6, constructor functions were used to create objects.&lt;/p&gt;

&lt;p&gt;function Person(name) {&lt;br&gt;
    this.name = name;&lt;br&gt;
}&lt;br&gt;
Person.prototype.greet=function (){&lt;br&gt;
                            console.log(this.name);&lt;br&gt;
                             }&lt;/p&gt;

&lt;p&gt;Now we can write:&lt;/p&gt;

&lt;p&gt;class Person {&lt;br&gt;
    constructor(name) {&lt;br&gt;
        this.name = name;&lt;br&gt;
    }&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;Both work almost the same internally.&lt;/p&gt;

&lt;p&gt;A class is simply a cleaner and easier syntax built on top of JavaScript's prototype system.&lt;/p&gt;

&lt;h1&gt;
  
  
  conclusion
&lt;/h1&gt;

&lt;p&gt;At first, JavaScript prototypes can feel confusing because there are terms like &lt;code&gt;prototype&lt;/code&gt;, &lt;code&gt;__proto__&lt;/code&gt;, constructor functions, and classes. But once I understood that objects inherit methods through prototypes, everything started making sense.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>blog</category>
      <category>learning</category>
      <category>fullstack</category>
    </item>
    <item>
      <title>My JavaScript Learning Journey:Understanding the JavaScript Event Loop and Concurrency Model</title>
      <dc:creator>HARSHITH GADDAM</dc:creator>
      <pubDate>Tue, 21 Jul 2026 13:31:11 +0000</pubDate>
      <link>https://dev.to/harshith_gaddam/my-javascript-learning-journeyunderstanding-the-javascript-event-loop-and-concurrency-model-4oec</link>
      <guid>https://dev.to/harshith_gaddam/my-javascript-learning-journeyunderstanding-the-javascript-event-loop-and-concurrency-model-4oec</guid>
      <description>&lt;p&gt;Today, I learned one of the most important concepts in JavaScript: the Event Loop and the Concurrency Model.&lt;/p&gt;

&lt;h1&gt;
  
  
  JavaScript is Single-Threaded
&lt;/h1&gt;

&lt;p&gt;JavaScript is a single-threaded language. This means it can execute only one piece of JavaScript code at a time.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;console.log("A");&lt;br&gt;
console.log("B");&lt;br&gt;
console.log("C");&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;A&lt;br&gt;
B&lt;br&gt;
C&lt;/p&gt;

&lt;p&gt;JavaScript executes these statements one after another.&lt;/p&gt;




&lt;h1&gt;
  
  
  What is the Call Stack?
&lt;/h1&gt;

&lt;p&gt;The Call Stack is where JavaScript executes functions. Whenever a function is called, it is added to the stack. After the function finishes executing, it is removed from the stack.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;function one() {&lt;br&gt;
    two();&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;function two() {&lt;br&gt;
    console.log("Hello");&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;one();&lt;/p&gt;

&lt;p&gt;Execution order:&lt;/p&gt;

&lt;p&gt;one()&lt;br&gt;
↓&lt;br&gt;
two()&lt;br&gt;
↓&lt;br&gt;
console.log()&lt;br&gt;
↓&lt;br&gt;
Stack becomes empty&lt;/p&gt;

&lt;p&gt;The Call Stack follows the Last In, First Out (LIFO) principle.&lt;/p&gt;

&lt;h1&gt;
  
  
  What are Web APIs and Node APIs?
&lt;/h1&gt;

&lt;p&gt;Some operations take time, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Waiting for a timer&lt;/li&gt;
&lt;li&gt;Making an API request&lt;/li&gt;
&lt;li&gt;Reading a file&lt;/li&gt;
&lt;li&gt;Listening for button clicks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;JavaScript itself cannot perform these operations.&lt;/p&gt;

&lt;p&gt;Instead, it asks the runtime to handle them.&lt;/p&gt;

&lt;h3&gt;
  
  
  In Browsers
&lt;/h3&gt;

&lt;p&gt;The browser provides Web APIs such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;setTimeout()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;setInterval()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fetch()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;addEventListener()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;DOM APIs&lt;/li&gt;
&lt;li&gt;&lt;code&gt;localStorage&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  In Node.js
&lt;/h3&gt;

&lt;p&gt;Node.js provides Node APIs such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;fs.readFile()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fs.writeFile()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;path&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;os&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;http&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These APIs perform asynchronous work outside the JavaScript engine.&lt;/p&gt;




&lt;h1&gt;
  
  
  Callback (Macrotask) Queue
&lt;/h1&gt;

&lt;p&gt;When an asynchronous operation like &lt;code&gt;setTimeout()&lt;/code&gt; finishes, its callback does not execute immediately.&lt;/p&gt;

&lt;p&gt;Instead, it is placed into the Callback Queue, also called the Macrotask Queue.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;setTimeout(() =&amp;gt; {&lt;br&gt;
    console.log("Timer");&lt;br&gt;
}, 1000);&lt;/p&gt;

&lt;p&gt;After one second Call back function added to Callback Queue.&lt;br&gt;
It waits there until the Event Loop moves it to the Call Stack.&lt;/p&gt;

&lt;h1&gt;
  
  
  Microtask Queue
&lt;/h1&gt;

&lt;p&gt;The Microtask Queue has a higher priority than the Callback Queue.&lt;/p&gt;

&lt;p&gt;The following callbacks are added to the Microtask Queue:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Promise.then()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Promise.catch()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Promise.finally()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;await&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;queueMicrotask()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;Promise.resolve().then(() =&amp;gt; {&lt;br&gt;
    console.log("Promise");&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;The callback is placed in the Microtask Queue.&lt;/p&gt;

&lt;h1&gt;
  
  
  Event Loop
&lt;/h1&gt;

&lt;p&gt;The Event Loop is responsible for checking whether the Call Stack is empty.&lt;/p&gt;

&lt;p&gt;When the Call Stack becomes empty, it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Executes all Microtasks&lt;/li&gt;
&lt;li&gt;Executes one Callback (Macrotask)&lt;/li&gt;
&lt;li&gt;Again checks call stack is empty or not and does 1&amp;amp;2.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It does not execute code itself. It simply moves callbacks from the queues to the Call Stack.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why do Promises execute before setTimeout()?
&lt;/h1&gt;

&lt;p&gt;Consider this example:&lt;/p&gt;

&lt;p&gt;console.log("Start");&lt;/p&gt;

&lt;p&gt;setTimeout(() =&amp;gt; {&lt;br&gt;
    console.log("Timeout");&lt;br&gt;
}, 0);&lt;/p&gt;

&lt;p&gt;Promise.resolve().then(() =&amp;gt; {&lt;br&gt;
    console.log("Promise");&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;console.log("End");&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
Start&lt;br&gt;
End&lt;br&gt;
Promise&lt;br&gt;
Timeout&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Promise.then()&lt;/code&gt; goes to the &lt;strong&gt;Microtask Queue&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setTimeout()&lt;/code&gt; goes to the &lt;strong&gt;Callback (Macrotask) Queue&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The Event Loop always executes &lt;strong&gt;all Microtasks first&lt;/strong&gt; before executing Macrotasks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Does setTimeout() execute exactly on time?
&lt;/h1&gt;

&lt;p&gt;A common misunderstanding is that:&lt;/p&gt;

&lt;p&gt;setTimeout(callback, 1000);&lt;/p&gt;

&lt;p&gt;means the callback will execute exactly after one second.&lt;/p&gt;

&lt;p&gt;This is not true.&lt;/p&gt;

&lt;p&gt;It means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Execute the callback after at least one second.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the Call Stack is busy with synchronous code, the callback has to wait.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;setTimeout(() =&amp;gt; {&lt;br&gt;
    console.log("Done");&lt;br&gt;
}, 1000);&lt;/p&gt;

&lt;p&gt;// Imagine this loop takes 3 seconds&lt;br&gt;
for (let i = 0; i &amp;lt; 1e10; i++) {}&lt;/p&gt;

&lt;p&gt;console.log("Finished");&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
Finished&lt;br&gt;
Done&lt;/p&gt;

&lt;p&gt;Even though the timer finished after one second, the callback waited until the Call Stack became empty.&lt;/p&gt;




&lt;h1&gt;
  
  
  What is the JavaScript Concurrency Model?
&lt;/h1&gt;

&lt;p&gt;JavaScript can execute only one line of code at a time, but it can still handle many asynchronous operations.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A timer can be counting.&lt;/li&gt;
&lt;li&gt;A network request can be downloading.&lt;/li&gt;
&lt;li&gt;The browser can listen for button clicks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All these operations are handled by the browser or Node.js while JavaScript continues executing other code.&lt;/p&gt;

&lt;p&gt;When these operations finish, their callbacks are added to the appropriate queue.&lt;/p&gt;

&lt;p&gt;This ability to make multiple operations progress during the same period of time is called the Concurrency Model.&lt;/p&gt;

&lt;h1&gt;
  
  
  Cooperative Concurrency vs True Parallelism
&lt;/h1&gt;

&lt;p&gt;These two terms are often confused.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cooperative Concurrency
&lt;/h3&gt;

&lt;p&gt;Only one JavaScript task runs at a time.&lt;/p&gt;

&lt;p&gt;Tasks take turns executing.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;One chef starts cooking rice, then makes tea while the rice cooks.&lt;/p&gt;

&lt;p&gt;The chef is not doing both tasks at the exact same moment, but both tasks are progressing.&lt;/p&gt;

&lt;p&gt;JavaScript works like this.&lt;/p&gt;

&lt;h3&gt;
  
  
  True Parallelism
&lt;/h3&gt;

&lt;p&gt;Multiple tasks execute at the exact same time using different CPU cores or threads.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chef 1 cooks rice.&lt;/li&gt;
&lt;li&gt;Chef 2 makes tea.&lt;/li&gt;
&lt;li&gt;Chef 3 cuts vegetables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All three are working simultaneously.&lt;/p&gt;

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

&lt;p&gt;Learning the Event Loop and the Concurrency Model helped me understand how JavaScript performs asynchronous operations without blocking the main thread. These concepts explain why Promise callbacks execute before &lt;code&gt;setTimeout()&lt;/code&gt;, why timers are not always exact, and how browsers and Node.js work together with JavaScript to build responsive applications.&lt;/p&gt;

&lt;p&gt;Understanding these fundamentals is essential for writing efficient JavaScript code.&lt;br&gt;
Thank you for reading! If you're also learning JavaScript, I hope this summary helps you understand these concepts more clearly. Happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>blog</category>
      <category>learning</category>
    </item>
    <item>
      <title>My JavaScript Learning Journey: Understanding How JavaScript Really Works</title>
      <dc:creator>HARSHITH GADDAM</dc:creator>
      <pubDate>Tue, 21 Jul 2026 11:32:29 +0000</pubDate>
      <link>https://dev.to/harshith_gaddam/my-javascript-learning-journey-understanding-how-javascript-really-works-37m8</link>
      <guid>https://dev.to/harshith_gaddam/my-javascript-learning-journey-understanding-how-javascript-really-works-37m8</guid>
      <description>&lt;p&gt;During this learning session, I focused on JavaScript's internal working, including Execution Context, Variables, Scope, Closures, Hoisting, this binding, Call Stack, and Clean Code Principles like DRY and KISS.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;JavaScript Execution Context&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first topic I learned was Execution Context.&lt;/p&gt;

&lt;p&gt;An Execution Context is the environment in which JavaScript executes code. Whenever JavaScript runs a script or calls a function, it creates an execution context.&lt;/p&gt;

&lt;p&gt;Each execution context has two phases:&lt;/p&gt;

&lt;p&gt;i)Memory Creation Phase&lt;br&gt;
     Before executing the code, JavaScript scans it and allocates memory.&lt;/p&gt;

&lt;p&gt;During this phase:&lt;/p&gt;

&lt;p&gt;var variables are initialized with undefined.&lt;br&gt;
let and const are allocated memory but remain uninitialized (Temporal Dead Zone).&lt;br&gt;
Function declarations are stored completely in memory.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;console.log(a);&lt;br&gt;
var a = 10;&lt;br&gt;
Output:&lt;br&gt;
undefined&lt;/p&gt;

&lt;p&gt;Because var is initialized with undefined during the memory phase.&lt;/p&gt;

&lt;p&gt;ii)Code Execution Phase&lt;/p&gt;

&lt;p&gt;After memory allocation, JavaScript starts executing the code line by line.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
var a = 10;&lt;br&gt;
console.log(a);&lt;br&gt;
Output:&lt;br&gt;
10&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Variables in JavaScript&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I learned the differences between var, let, and const.&lt;/p&gt;

&lt;p&gt;a)var&lt;br&gt;
-Function scoped&lt;br&gt;
-Can be redeclared&lt;br&gt;
-Can be reassigned&lt;br&gt;
-Hoisted with undefined&lt;/p&gt;

&lt;p&gt;b)let&lt;br&gt;
Block scoped&lt;br&gt;
Cannot be redeclared in the same scope&lt;br&gt;
Can be reassigned&lt;br&gt;
Exists in the Temporal Dead Zone before initialization&lt;/p&gt;

&lt;p&gt;c)const&lt;br&gt;
Block scoped&lt;br&gt;
Cannot be redeclared&lt;br&gt;
Cannot be reassigned&lt;br&gt;
Must be initialized during declaration&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lexical Scope&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One important concept I learned is Lexical Scope.&lt;/p&gt;

&lt;p&gt;Lexical Scope means a function can access variables from the scope where it was created.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;let name = "Harshith";&lt;/p&gt;

&lt;p&gt;function greet() {&lt;br&gt;
    console.log(name);&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;greet();&lt;/p&gt;

&lt;p&gt;The function accesses name because it was created inside the same scope.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Closures&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A Closure is created when a function remembers variables from its outer scope even after the outer function has finished executing.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;function counter() {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let count = 0;

return function () {
    count++;
    console.log(count);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;const increment = counter();&lt;/p&gt;

&lt;p&gt;increment();&lt;br&gt;
increment();&lt;br&gt;
increment();&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
1&lt;br&gt;
2&lt;br&gt;
3&lt;/p&gt;

&lt;p&gt;The variable count remains available because of the closure.&lt;/p&gt;

&lt;p&gt;Closures are commonly used to create private variables and implement the module pattern.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Hoisting&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hoisting is JavaScript's behavior of moving declarations to the top of their scope before execution.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;console.log(a);&lt;br&gt;
var a = 10;&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
undefined&lt;/p&gt;

&lt;p&gt;For let and const:&lt;/p&gt;

&lt;p&gt;console.log(a);&lt;br&gt;
let a = 10;&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
ReferenceError&lt;/p&gt;

&lt;p&gt;because the variable is inside the Temporal Dead Zone until it is initialized.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Understanding this Binding&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One of the most interesting topics I learned was how JavaScript decides the value of this.&lt;/p&gt;

&lt;p&gt;a)Default Binding&lt;br&gt;
function greet() {&lt;br&gt;
    console.log(this);&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;greet();&lt;/p&gt;

&lt;p&gt;In non-strict mode, this refers to the global object.&lt;/p&gt;

&lt;p&gt;b)Implicit Binding&lt;br&gt;
const person = {&lt;br&gt;
    name: "Harshith",&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;person.greet();&lt;/p&gt;

&lt;p&gt;Here, this refers to person.&lt;/p&gt;

&lt;p&gt;c)Explicit Binding&lt;/p&gt;

&lt;p&gt;JavaScript allows us to manually set this using call(), apply(), and bind().&lt;/p&gt;

&lt;p&gt;function greet(city) {&lt;br&gt;
    console.log(this.name, city);&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;const person = {&lt;br&gt;
    name: "Harshith"&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;greet.call(person, "Hyderabad");&lt;/p&gt;

&lt;p&gt;d)new Binding&lt;/p&gt;

&lt;p&gt;When a function is called using the new keyword, JavaScript creates a new object and binds this to it.&lt;/p&gt;

&lt;p&gt;function Person(name) {&lt;br&gt;
    this.name = name;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;const p = new Person("Harsh");&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Writing Better Code with DRY and KISS&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Besides JavaScript internals, I also learned two important software engineering principles.&lt;/p&gt;

&lt;p&gt;a)DRY (Don't Repeat Yourself)&lt;/p&gt;

&lt;p&gt;Instead of repeating the same logic multiple times, create reusable functions.&lt;/p&gt;

&lt;p&gt;This makes code easier to maintain.&lt;/p&gt;

&lt;p&gt;b)KISS (Keep It Simple, Stupid)&lt;/p&gt;

&lt;p&gt;Write simple, readable code instead of overly complex solutions.&lt;/p&gt;

&lt;p&gt;Simple code is easier to understand, debug, and maintain.&lt;/p&gt;

&lt;p&gt;Thank you for reading! If you're also learning JavaScript, I hope this summary helps you understand these core concepts more clearly. Happy coding! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>blog</category>
    </item>
  </channel>
</rss>
