<?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>SQL and Database Fundamentals: From Tables to Transactions and Indexes</title>
      <dc:creator>HARSHITH GADDAM</dc:creator>
      <pubDate>Fri, 09 Oct 2026 09:48:22 +0000</pubDate>
      <link>https://dev.to/harshith_gaddam/sql-and-database-fundamentals-from-tables-to-transactions-and-indexes-4i0p</link>
      <guid>https://dev.to/harshith_gaddam/sql-and-database-fundamentals-from-tables-to-transactions-and-indexes-4i0p</guid>
      <description>&lt;h1&gt;
  
  
  SQL and Database Fundamentals: From Tables to Transactions and Indexes
&lt;/h1&gt;

&lt;p&gt;When I started learning databases, I realized that writing SQL queries&lt;br&gt;
is only one part of working with a database. To work with data properly,&lt;br&gt;
we also need to understand how tables are designed, how they are&lt;br&gt;
connected, how duplicate data can be avoided, and how database&lt;br&gt;
operations are kept safe.&lt;/p&gt;

&lt;p&gt;In this blog, I am explaining the database concepts I have learned, with&lt;br&gt;
simple examples and SQL queries.&lt;/p&gt;

&lt;p&gt;We will cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Tables, primary keys, and foreign keys&lt;/li&gt;
&lt;li&gt;  Relationships between tables&lt;/li&gt;
&lt;li&gt;  SQL joins: implicit joins vs.&amp;nbsp;explicit joins, INNER/LEFT/RIGHT/FULL&lt;/li&gt;
&lt;li&gt;  Aggregation functions&lt;/li&gt;
&lt;li&gt;  Normalization: 1NF, 2NF, 3NF, BCNF, 4NF, and 5NF&lt;/li&gt;
&lt;li&gt;  Transactions: &lt;code&gt;BEGIN&lt;/code&gt;, &lt;code&gt;COMMIT&lt;/code&gt;, and &lt;code&gt;ROLLBACK&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  Primary, unique, and composite indexes&lt;/li&gt;
&lt;li&gt;  Understanding &lt;code&gt;EXPLAIN&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's start with the basics.&lt;/p&gt;
&lt;h2&gt;
  
  
  1. Tables: How Databases Store Data
&lt;/h2&gt;

&lt;p&gt;A relational database stores data in tables. A table contains rows and&lt;br&gt;
columns.&lt;/p&gt;

&lt;p&gt;For example, imagine we are building an online shopping application.&lt;/p&gt;

&lt;p&gt;We can create a table to store customer information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;150&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, let's insert some data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Rahul'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'rahul@example.com'&lt;/span&gt;&lt;span class="p"&gt;),&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="s1"&gt;'Priya'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'priya@example.com'&lt;/span&gt;&lt;span class="p"&gt;),&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="s1"&gt;'Arun'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'arun@example.com'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The table now looks like this:&lt;/p&gt;

&lt;p&gt;customer_id   name    email&lt;/p&gt;




&lt;p&gt;1             Rahul   &lt;a href="mailto:rahul@example.com"&gt;rahul@example.com&lt;/a&gt;&lt;br&gt;
  2             Priya   &lt;a href="mailto:priya@example.com"&gt;priya@example.com&lt;/a&gt;&lt;br&gt;
  3             Arun    &lt;a href="mailto:arun@example.com"&gt;arun@example.com&lt;/a&gt;&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Table:&lt;/strong&gt; &lt;code&gt;customers&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Columns:&lt;/strong&gt; &lt;code&gt;customer_id&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, and &lt;code&gt;email&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Rows:&lt;/strong&gt; Individual customer records&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can retrieve the data using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&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;*&lt;/code&gt; means we want to retrieve all columns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I understood:&lt;/strong&gt; Tables organize related information into rows and&lt;br&gt;
columns, making it easier to store, retrieve, and manage data.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. Primary Keys and Foreign Keys
&lt;/h2&gt;

&lt;p&gt;When a database contains many tables, we need a way to identify&lt;br&gt;
individual records and connect related records.&lt;/p&gt;

&lt;p&gt;This is where primary keys and foreign keys become important.&lt;/p&gt;
&lt;h3&gt;
  
  
  Primary Key (PK)
&lt;/h3&gt;

&lt;p&gt;A primary key uniquely identifies each row in a table.&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 sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;product_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;product_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="nb"&gt;NUMERIC&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="mi"&gt;2&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;product_id&lt;/code&gt; is the primary key.&lt;/p&gt;

&lt;p&gt;It ensures that: - Every product has a unique ID. - The ID cannot be&lt;br&gt;
&lt;code&gt;NULL&lt;/code&gt;. - Two products cannot have the same ID.&lt;/p&gt;

&lt;p&gt;For example, we cannot insert two products with &lt;code&gt;product_id = 101&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A table has only one primary key constraint, but that key can contain&lt;br&gt;
multiple columns. We will see this later.&lt;/p&gt;
&lt;h3&gt;
  
  
  Foreign Key (FK)
&lt;/h3&gt;

&lt;p&gt;A foreign key connects one table to another by referencing a primary key&lt;br&gt;
or another eligible unique key.&lt;/p&gt;

&lt;p&gt;Let's create an &lt;code&gt;orders&lt;/code&gt; table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;order_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;order_date&lt;/span&gt; &lt;span class="nb"&gt;DATE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;FOREIGN&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&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;customer_id&lt;/code&gt; is a foreign key.&lt;/p&gt;

&lt;p&gt;Suppose we insert an order for customer ID &lt;code&gt;1&lt;/code&gt;. It works because&lt;br&gt;
customer &lt;code&gt;1&lt;/code&gt; exists in the &lt;code&gt;customers&lt;/code&gt; table.&lt;/p&gt;

&lt;p&gt;However, an order for customer ID &lt;code&gt;99&lt;/code&gt; will fail if customer &lt;code&gt;99&lt;/code&gt; does&lt;br&gt;
not exist.&lt;/p&gt;

&lt;p&gt;The foreign key protects the relationship between the tables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important difference:&lt;/strong&gt;&lt;/p&gt;



&lt;p&gt;Primary key                         Foreign key&lt;/p&gt;



&lt;p&gt;Identifies a row uniquely           Connects related records&lt;/p&gt;

&lt;p&gt;Cannot contain &lt;code&gt;NULL&lt;/code&gt;               Can contain &lt;code&gt;NULL&lt;/code&gt; unless&lt;br&gt;
                                      restricted&lt;/p&gt;

&lt;p&gt;Must be unique                      Can contain duplicate values&lt;/p&gt;

&lt;p&gt;One primary key constraint per      A table can have multiple foreign&lt;br&gt;
  table                               keys&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;What I understood:&lt;/strong&gt; A primary key identifies a record, while a&lt;br&gt;
foreign key helps maintain valid relationships between records.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Relationships Between Tables
&lt;/h2&gt;

&lt;p&gt;In real applications, data is divided into multiple tables because&lt;br&gt;
different types of information have different purposes.&lt;/p&gt;

&lt;p&gt;There are three common types of relationships.&lt;/p&gt;
&lt;h3&gt;
  
  
  3.1 One-to-One (1:1)
&lt;/h3&gt;

&lt;p&gt;One record in the first table is associated with at most one record in&lt;br&gt;
the second table, and vice versa.&lt;/p&gt;

&lt;p&gt;Example: A user and their profile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;user_profiles&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;profile_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;
        &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;bio&lt;/span&gt; &lt;span class="nb"&gt;TEXT&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;UNIQUE&lt;/code&gt; constraint on &lt;code&gt;customer_id&lt;/code&gt; prevents the same customer from&lt;br&gt;
having multiple profile records.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;  Customer 1 → Profile 101&lt;/li&gt;
&lt;li&gt;  Customer 2 → Profile 102&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A customer can have no profile or one profile. The &lt;code&gt;UNIQUE&lt;/code&gt; constraint&lt;br&gt;
ensures that a customer cannot be linked to multiple profiles through&lt;br&gt;
this column.&lt;/p&gt;
&lt;h3&gt;
  
  
  3.2 One-to-Many (1:N)
&lt;/h3&gt;

&lt;p&gt;One record in the first table can be associated with many records in the&lt;br&gt;
second table.&lt;/p&gt;

&lt;p&gt;Example: One customer can place many orders.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;customers
---------
customer_id: 1
name: Rahul

orders
------
order_id: 101, customer_id: 1
order_id: 102, customer_id: 1
order_id: 103, customer_id: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Customer Rahul has placed three orders.&lt;/p&gt;

&lt;p&gt;We implement this relationship by placing the foreign key in the&lt;br&gt;
&lt;code&gt;orders&lt;/code&gt; table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The foreign key can repeat because many orders can belong to the same&lt;br&gt;
customer.&lt;/p&gt;
&lt;h3&gt;
  
  
  3.3 Many-to-Many (N:N)
&lt;/h3&gt;

&lt;p&gt;Many records in one table can be associated with many records in another&lt;br&gt;
table.&lt;/p&gt;

&lt;p&gt;Example: Students and courses.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  One student can enroll in many courses.&lt;/li&gt;
&lt;li&gt;  One course can have many students.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We use a junction table to represent this relationship.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;student_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;courses&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;course_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;course_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;enrollments&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;course_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;courses&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;course_id&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;course_id&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;enrollments&lt;/code&gt; table connects students and courses.&lt;/p&gt;

&lt;p&gt;student_id   course_id&lt;/p&gt;




&lt;p&gt;1            101&lt;br&gt;
  1            102&lt;br&gt;
  2            101&lt;/p&gt;

&lt;p&gt;Student 1 is enrolled in two courses, while course 101 has two students.&lt;/p&gt;

&lt;p&gt;The composite primary key prevents the same student from being enrolled&lt;br&gt;
in the same course more than once.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I understood:&lt;/strong&gt; A 1:1 relationship often uses a foreign key with&lt;br&gt;
a unique constraint, a 1:N relationship uses a foreign key on the many&lt;br&gt;
side, and an N:N relationship uses a junction table.&lt;/p&gt;
&lt;h2&gt;
  
  
  4. SQL Joins: Implicit and Explicit Joins
&lt;/h2&gt;

&lt;p&gt;We often store related information in separate tables. A &lt;code&gt;JOIN&lt;/code&gt; lets us&lt;br&gt;
retrieve related data from multiple tables in one query.&lt;/p&gt;

&lt;p&gt;In SQL, explicit and implicit joins are two different syntactical styles used to combine data from two or more tables based on a related column. Both produce the exact same query result and execution plan in modern database engines, but they differ in readability, maintenance, and standards.&lt;/p&gt;

&lt;p&gt;Let's use our &lt;code&gt;customers&lt;/code&gt; and &lt;code&gt;orders&lt;/code&gt; tables.&lt;/p&gt;
&lt;h3&gt;
  
  
  4.1 Implicit JOIN
&lt;/h3&gt;

&lt;p&gt;An implicit join uses a comma to list tables in the &lt;code&gt;FROM&lt;/code&gt; clause. The&lt;br&gt;
matching condition is written in the &lt;code&gt;WHERE&lt;/code&gt; clause.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_date&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The condition &lt;code&gt;customers.customer_id = orders.customer_id&lt;/code&gt; tells SQL&lt;br&gt;
which customer matches each order.&lt;/p&gt;

&lt;p&gt;This query returns matching customer-order rows, similar to an&lt;br&gt;
&lt;code&gt;INNER JOIN&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What can go wrong?&lt;/strong&gt; If we forget the matching condition, the database&lt;br&gt;
combines every customer with every order. This is called a Cartesian&lt;br&gt;
product. For example, 3 customers and 10 orders can produce 30 rows,&lt;br&gt;
even when most of those pairs are unrelated.&lt;/p&gt;

&lt;p&gt;Implicit join syntax is older and can become difficult to read when a&lt;br&gt;
query uses several tables and conditions.&lt;/p&gt;
&lt;h3&gt;
  
  
  4.2 Explicit JOIN
&lt;/h3&gt;

&lt;p&gt;An explicit join uses the &lt;code&gt;JOIN&lt;/code&gt; keyword and puts the matching condition&lt;br&gt;
next to the tables being joined.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_date&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This query produces the same matching rows as the previous implicit&lt;br&gt;
join.&lt;/p&gt;

&lt;p&gt;Here: - &lt;code&gt;INNER JOIN&lt;/code&gt; says that we want matching rows. - &lt;code&gt;ON&lt;/code&gt; defines how&lt;br&gt;
the two tables are related.&lt;/p&gt;
&lt;h3&gt;
  
  
  Why do we prefer explicit JOINs?
&lt;/h3&gt;

&lt;p&gt;Both forms can express an inner join, but explicit joins are usually&lt;br&gt;
better because:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;They are easier to read.&lt;/strong&gt; The relationship between tables is
written directly beside the &lt;code&gt;JOIN&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;They reduce mistakes.&lt;/strong&gt; Join conditions are kept separate from
other filters, so it is easier to notice a missing condition.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;They support outer joins clearly.&lt;/strong&gt; &lt;code&gt;LEFT JOIN&lt;/code&gt;, &lt;code&gt;RIGHT JOIN&lt;/code&gt;, and
&lt;code&gt;FULL OUTER JOIN&lt;/code&gt; are expressed naturally with explicit syntax.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;They are easier to maintain.&lt;/strong&gt; When a query contains several
tables, the join logic is easier to follow.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Explicit joins do not automatically make a query faster. The database&lt;br&gt;
optimizer may produce the same execution plan for equivalent queries.&lt;br&gt;
The main benefits are clarity and safer query writing.&lt;/p&gt;
&lt;h3&gt;
  
  
  4.3 INNER JOIN
&lt;/h3&gt;

&lt;p&gt;An &lt;code&gt;INNER JOIN&lt;/code&gt; returns only rows that match in both tables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If Rahul has two orders and Priya has one order, the result contains&lt;br&gt;
three rows. If Arun has no orders, Arun does not appear.&lt;/p&gt;
&lt;h3&gt;
  
  
  4.4 LEFT JOIN
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;LEFT JOIN&lt;/code&gt; returns every row from the left table and matching rows&lt;br&gt;
from the right table. If there is no match, the right-side columns&lt;br&gt;
contain &lt;code&gt;NULL&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Arun appears even if he has no orders. His &lt;code&gt;order_id&lt;/code&gt; is &lt;code&gt;NULL&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is useful when we want to find all customers, including those who&lt;br&gt;
have never placed an order.&lt;/p&gt;
&lt;h3&gt;
  
  
  4.5 RIGHT JOIN
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;RIGHT JOIN&lt;/code&gt; returns every row from the right table and matching rows&lt;br&gt;
from the left table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;
&lt;span class="k"&gt;RIGHT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every order appears, along with its matching customer information. In&lt;br&gt;
this example, the foreign key means every non-null customer ID in&lt;br&gt;
&lt;code&gt;orders&lt;/code&gt; must refer to an existing customer.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;RIGHT JOIN&lt;/code&gt; can often be rewritten as a &lt;code&gt;LEFT JOIN&lt;/code&gt; by switching the&lt;br&gt;
table order.&lt;/p&gt;
&lt;h3&gt;
  
  
  4.6 FULL OUTER JOIN
&lt;/h3&gt;

&lt;p&gt;A &lt;code&gt;FULL OUTER JOIN&lt;/code&gt; returns matching rows, unmatched rows from the left&lt;br&gt;
table, and unmatched rows from the right table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;
&lt;span class="k"&gt;FULL&lt;/span&gt; &lt;span class="k"&gt;OUTER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is useful when we want to see all records from both tables, even&lt;br&gt;
when a matching record does not exist.&lt;/p&gt;
&lt;h3&gt;
  
  
  Quick comparison
&lt;/h3&gt;



&lt;p&gt;Join                                What it returns&lt;/p&gt;



&lt;p&gt;Implicit join                       Older comma-separated syntax;&lt;br&gt;
                                      matching conditions are usually in&lt;br&gt;
                                      &lt;code&gt;WHERE&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;INNER JOIN&lt;/code&gt;                        Only matching rows&lt;/p&gt;

&lt;p&gt;&lt;code&gt;LEFT JOIN&lt;/code&gt;                         All left rows and matching right&lt;br&gt;
                                      rows&lt;/p&gt;

&lt;p&gt;&lt;code&gt;RIGHT JOIN&lt;/code&gt;                        All right rows and matching left&lt;br&gt;
                                      rows&lt;/p&gt;

&lt;p&gt;&lt;code&gt;FULL OUTER JOIN&lt;/code&gt;                   All rows from both sides&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;What I understood:&lt;/strong&gt; Implicit and explicit syntax can produce the same&lt;br&gt;
result for an inner join, but explicit joins make the relationship&lt;br&gt;
clearer and are easier to maintain. The type of join determines what&lt;br&gt;
happens to rows that have no match.&lt;/p&gt;
&lt;h2&gt;
  
  
  5. Aggregation Functions
&lt;/h2&gt;

&lt;p&gt;Sometimes, retrieving individual rows is not enough. We may want to&lt;br&gt;
calculate totals, averages, or counts.&lt;/p&gt;

&lt;p&gt;SQL provides aggregate functions for this purpose.&lt;/p&gt;

&lt;p&gt;The most common ones are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;COUNT()&lt;/code&gt; --- counts rows or non-null values, depending on the
expression.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;SUM()&lt;/code&gt; --- calculates a total.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;AVG()&lt;/code&gt; --- calculates an average.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;MIN()&lt;/code&gt; --- finds the smallest value.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;MAX()&lt;/code&gt; --- finds the largest value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Suppose we have these orders:&lt;/p&gt;

&lt;p&gt;order_id   customer_id     total_amount&lt;/p&gt;



&lt;p&gt;101        1                        500&lt;br&gt;
  102        1                        300&lt;br&gt;
  103        2                        700&lt;/p&gt;
&lt;h3&gt;
  
  
  Count the orders
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;COUNT&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="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_orders&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Result: &lt;code&gt;3&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Calculate total sales
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_sales&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This requires a &lt;code&gt;total_amount&lt;/code&gt; column in the &lt;code&gt;orders&lt;/code&gt; table. If we add&lt;br&gt;
that column and use the values above, the result is &lt;code&gt;1500&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Calculate average order value
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;average_order_value&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The result is &lt;code&gt;500&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  GROUP BY
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;GROUP BY&lt;/code&gt; groups rows with the same value so that we can calculate a&lt;br&gt;
separate result for each group.&lt;/p&gt;

&lt;p&gt;For example, we want to know how much each customer has spent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_spent&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;p&gt;customer_id     total_spent&lt;/p&gt;




&lt;p&gt;1                       800&lt;br&gt;
  2                       700&lt;/p&gt;

&lt;p&gt;Instead of calculating one total for all orders, SQL calculates a&lt;br&gt;
separate total for each customer.&lt;/p&gt;
&lt;h3&gt;
  
  
  HAVING
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;HAVING&lt;/code&gt; filters groups after aggregation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total_spent&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;600&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This returns only customers whose total spending is greater than 600.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I understood:&lt;/strong&gt; Aggregate functions calculate summary values,&lt;br&gt;
&lt;code&gt;GROUP BY&lt;/code&gt; creates groups, and &lt;code&gt;HAVING&lt;/code&gt; filters those groups based on&lt;br&gt;
aggregate results.&lt;/p&gt;
&lt;h2&gt;
  
  
  6. Database Normalization: 1NF, 2NF, 3NF, BCNF, 4NF, and 5NF
&lt;/h2&gt;

&lt;p&gt;Normalization is the process of organizing data into tables to reduce&lt;br&gt;
unnecessary duplication and avoid data problems.&lt;/p&gt;

&lt;p&gt;Imagine storing customer information and all their orders in one table.&lt;br&gt;
Rahul's name may be repeated for every order. If his name changes, we&lt;br&gt;
must update several rows. If we update only some of them, the data&lt;br&gt;
becomes inconsistent.&lt;/p&gt;

&lt;p&gt;Normalization helps us place each fact in the right table. Let's go&lt;br&gt;
through the normal forms one by one.&lt;/p&gt;
&lt;h3&gt;
  
  
  6.1 First Normal Form (1NF)
&lt;/h3&gt;

&lt;p&gt;A table is in First Normal Form when each cell contains a single value&lt;br&gt;
rather than a list of values, and rows can be identified individually.&lt;/p&gt;

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

&lt;p&gt;student_id   student_name   courses&lt;/p&gt;



&lt;p&gt;1            Rahul          Java, SQL&lt;br&gt;
  2            Priya          Python, Java&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;courses&lt;/code&gt; column contains multiple values in one cell.&lt;/p&gt;

&lt;p&gt;We can instead store one student-course combination per row:&lt;/p&gt;

&lt;p&gt;student_id   student_name   course&lt;/p&gt;



&lt;p&gt;1            Rahul          Java&lt;br&gt;
  1            Rahul          SQL&lt;br&gt;
  2            Priya          Python&lt;br&gt;
  2            Priya          Java&lt;/p&gt;

&lt;p&gt;Now each cell contains one value. In a real application, we would&lt;br&gt;
usually separate students and courses into their own tables and connect&lt;br&gt;
them with an enrollment table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Main idea:&lt;/strong&gt; Each cell should contain one value, not a list of&lt;br&gt;
separate values.&lt;/p&gt;
&lt;h3&gt;
  
  
  6.2 Second Normal Form (2NF)
&lt;/h3&gt;

&lt;p&gt;A table is in 2NF when: 1. It is already in 1NF. 2. Every non-key column&lt;br&gt;
depends on the whole of every candidate key, not just part of a&lt;br&gt;
composite key.&lt;/p&gt;

&lt;p&gt;Consider this table, where the primary key is &lt;code&gt;(student_id, course_id)&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;student_id   course_id   student_name   course_name&lt;/p&gt;



&lt;p&gt;1            101         Rahul          SQL&lt;br&gt;
  1            102         Rahul          Java&lt;br&gt;
  2            101         Priya          SQL&lt;/p&gt;

&lt;p&gt;&lt;code&gt;student_name&lt;/code&gt; depends only on &lt;code&gt;student_id&lt;/code&gt;, and &lt;code&gt;course_name&lt;/code&gt; depends&lt;br&gt;
only on &lt;code&gt;course_id&lt;/code&gt;. Neither depends on the full two-column key. These&lt;br&gt;
are partial dependencies.&lt;/p&gt;

&lt;p&gt;We can split the data into three tables:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Students&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;student_id   student_name&lt;/p&gt;



&lt;p&gt;1            Rahul&lt;br&gt;
  2            Priya&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Courses&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;course_id   course_name&lt;/p&gt;



&lt;p&gt;101         SQL&lt;br&gt;
  102         Java&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enrollments&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;student_id   course_id&lt;/p&gt;



&lt;p&gt;1            101&lt;br&gt;
  1            102&lt;br&gt;
  2            101&lt;/p&gt;

&lt;p&gt;Now student details belong in &lt;code&gt;students&lt;/code&gt;, course details belong in&lt;br&gt;
&lt;code&gt;courses&lt;/code&gt;, and enrollment records use the combination of both IDs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Main idea:&lt;/strong&gt; If a candidate key contains multiple columns, non-key&lt;br&gt;
facts should not depend on only part of that key.&lt;/p&gt;
&lt;h3&gt;
  
  
  6.3 Third Normal Form (3NF)
&lt;/h3&gt;

&lt;p&gt;A table is in 3NF when it is in 2NF and non-key attributes do not depend&lt;br&gt;
transitively on a candidate key. More precisely, for every non-trivial&lt;br&gt;
functional dependency &lt;code&gt;X → A&lt;/code&gt;, either &lt;code&gt;X&lt;/code&gt; is a superkey or &lt;code&gt;A&lt;/code&gt; is a&lt;br&gt;
prime attribute (an attribute that belongs to at least one candidate&lt;br&gt;
key).&lt;/p&gt;

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

&lt;p&gt;employee_id   employee_name   department_id   department_name&lt;/p&gt;



&lt;p&gt;1             Rahul           10              Engineering&lt;br&gt;
  2             Priya           10              Engineering&lt;br&gt;
  3             Arun            20              Marketing&lt;/p&gt;

&lt;p&gt;Assume &lt;code&gt;employee_id&lt;/code&gt; is the key and each department ID determines one&lt;br&gt;
department name.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;employee_id&lt;/code&gt; determines &lt;code&gt;department_id&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;department_id&lt;/code&gt; determines &lt;code&gt;department_name&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, the department name depends on the employee ID indirectly through&lt;br&gt;
&lt;code&gt;department_id&lt;/code&gt;. This is a transitive dependency.&lt;/p&gt;

&lt;p&gt;We can split the data into:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Employees&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;employee_id   employee_name   department_id&lt;/p&gt;



&lt;p&gt;1             Rahul           10&lt;br&gt;
  2             Priya           10&lt;br&gt;
  3             Arun            20&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Departments&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;department_id   department_name&lt;/p&gt;



&lt;p&gt;10              Engineering&lt;br&gt;
  20              Marketing&lt;/p&gt;

&lt;p&gt;Now the department name is stored once in the &lt;code&gt;departments&lt;/code&gt; table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Main idea:&lt;/strong&gt; Avoid storing facts about one entity repeatedly in a&lt;br&gt;
table mainly about another entity.&lt;/p&gt;
&lt;h3&gt;
  
  
  6.4 Boyce-Codd Normal Form (BCNF)
&lt;/h3&gt;

&lt;p&gt;BCNF is a stronger version of 3NF. A table is in BCNF when, for every&lt;br&gt;
non-trivial functional dependency &lt;code&gt;X → Y&lt;/code&gt;, &lt;code&gt;X&lt;/code&gt; must be a superkey.&lt;/p&gt;

&lt;p&gt;In simple words, &lt;strong&gt;every column or group of columns that determines&lt;br&gt;
another fact must be able to identify a row uniquely&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Consider a table with these columns:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Student, Course, Instructor&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Assume: - A student can study a course with an instructor. - Each&lt;br&gt;
instructor teaches only one course. - A student can have more than one&lt;br&gt;
instructor.&lt;/p&gt;

&lt;p&gt;The important dependency is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Instructor → Course&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The instructor determines the course, but &lt;code&gt;Instructor&lt;/code&gt; alone does not&lt;br&gt;
identify a student-instructor row because many students can have the&lt;br&gt;
same instructor. So the determinant is not a superkey, and the table&lt;br&gt;
violates BCNF.&lt;/p&gt;

&lt;p&gt;We can separate the facts into tables such as:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;InstructorCourses&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;instructor   course&lt;/p&gt;



&lt;p&gt;I1           SQL&lt;br&gt;
  I2           Java&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;StudentInstructors&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;student   instructor&lt;/p&gt;



&lt;p&gt;Rahul     I1&lt;br&gt;
  Priya     I1&lt;br&gt;
  Rahul     I2&lt;/p&gt;

&lt;p&gt;This stores which course an instructor teaches separately from which&lt;br&gt;
instructor teaches a student.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Main idea:&lt;/strong&gt; In BCNF, every determinant must be a superkey. BCNF&lt;br&gt;
handles some dependency problems that 3NF can still allow.&lt;/p&gt;
&lt;h3&gt;
  
  
  6.5 Fourth Normal Form (4NF)
&lt;/h3&gt;

&lt;p&gt;4NF deals with a different problem: independent multi-valued facts in&lt;br&gt;
the same table.&lt;/p&gt;

&lt;p&gt;Imagine a student can have several hobbies and speak several languages.&lt;br&gt;
Assume hobbies and languages are independent of each other.&lt;/p&gt;

&lt;p&gt;A single table might look like this:&lt;/p&gt;

&lt;p&gt;student   hobby     language&lt;/p&gt;



&lt;p&gt;Rahul     Cricket   English&lt;br&gt;
  Rahul     Cricket   Telugu&lt;br&gt;
  Rahul     Music     English&lt;br&gt;
  Rahul     Music     Telugu&lt;/p&gt;

&lt;p&gt;Every hobby is repeated for every language. Every language is repeated&lt;br&gt;
for every hobby. If Rahul adds a hobby, we may need to add a row for&lt;br&gt;
each language.&lt;/p&gt;

&lt;p&gt;We can separate these independent facts into two tables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;StudentHobbies&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;student   hobby&lt;/p&gt;



&lt;p&gt;Rahul     Cricket&lt;br&gt;
  Rahul     Music&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;StudentLanguages&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;student   language&lt;/p&gt;



&lt;p&gt;Rahul     English&lt;br&gt;
  Rahul     Telugu&lt;/p&gt;

&lt;p&gt;A table is in 4NF when it is in BCNF and has no non-trivial multivalued&lt;br&gt;
dependency unless its determinant is a superkey.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Main idea:&lt;/strong&gt; Keep independent multi-valued facts in separate tables&lt;br&gt;
instead of storing every possible combination.&lt;/p&gt;
&lt;h3&gt;
  
  
  6.6 Fifth Normal Form (5NF)
&lt;/h3&gt;

&lt;p&gt;5NF deals with join dependencies. It helps when a table can be split&lt;br&gt;
into smaller tables and reconstructed exactly by joining them, without&lt;br&gt;
creating incorrect combinations.&lt;/p&gt;

&lt;p&gt;Imagine a business records which suppliers provide which parts for which&lt;br&gt;
projects. The table has three columns:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Supplier, Part, Project&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In some systems, the business rules may mean that a valid&lt;br&gt;
supplier-part-project combination can be determined entirely from three&lt;br&gt;
pairwise relationships:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Which parts a supplier can provide&lt;/li&gt;
&lt;li&gt;  Which projects a supplier works on&lt;/li&gt;
&lt;li&gt;  Which parts a project uses&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If---and only if---the business rules guarantee that these pairwise&lt;br&gt;
relationships are enough to determine every valid three-way combination,&lt;br&gt;
we can store them in three tables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;SupplierParts(Supplier, Part)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;SupplierProjects(Supplier, Project)&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;code&gt;ProjectParts(Project, Part)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Joining the three tables can reconstruct the original combinations&lt;br&gt;
without adding invalid combinations.&lt;/p&gt;

&lt;p&gt;This decomposition is not safe for every supplier-part-project dataset.&lt;br&gt;
It is valid only when the required join dependency holds.&lt;/p&gt;

&lt;p&gt;A table is in 5NF (also called Project-Join Normal Form) when every&lt;br&gt;
non-trivial join dependency is implied by its candidate keys.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Main idea:&lt;/strong&gt; 5NF handles cases where a table can be split into smaller&lt;br&gt;
relationship tables and joined back together without losing information&lt;br&gt;
or creating extra rows.&lt;/p&gt;
&lt;h3&gt;
  
  
  Quick comparison of normal forms
&lt;/h3&gt;



&lt;p&gt;Normal form                         Main idea&lt;/p&gt;



&lt;p&gt;1NF                                 Each cell contains a single value&lt;/p&gt;

&lt;p&gt;2NF                                 No partial dependency on part of a&lt;br&gt;
                                      candidate key&lt;/p&gt;

&lt;p&gt;3NF                                 No disallowed transitive dependency&lt;br&gt;
                                      of non-key attributes&lt;/p&gt;

&lt;p&gt;BCNF                                Every determinant in a non-trivial&lt;br&gt;
                                      functional dependency is a superkey&lt;/p&gt;

&lt;p&gt;4NF                                 No non-trivial multivalued&lt;br&gt;
                                      dependency unless its determinant&lt;br&gt;
                                      is a superkey&lt;/p&gt;

&lt;p&gt;5NF                                 No non-trivial join dependency&lt;br&gt;
                                      remains except those implied by&lt;br&gt;
                                      candidate keys&lt;/p&gt;



&lt;p&gt;In practice, 1NF through 3NF are common starting points for relational&lt;br&gt;
database design. BCNF, 4NF, and 5NF address more specific dependency&lt;br&gt;
problems. The right design depends on the real business rules; splitting&lt;br&gt;
tables without understanding those rules can cause incorrect results&lt;br&gt;
when the tables are joined again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I understood:&lt;/strong&gt; Normalization is about understanding which facts&lt;br&gt;
depend on which keys or relationships, then organizing tables so that&lt;br&gt;
each fact is stored in the right place with less unnecessary repetition.&lt;/p&gt;
&lt;h2&gt;
  
  
  7. Transactions: BEGIN, COMMIT, and ROLLBACK
&lt;/h2&gt;

&lt;p&gt;A transaction is a group of database operations treated as one unit of&lt;br&gt;
work.&lt;/p&gt;

&lt;p&gt;Consider a bank transfer.&lt;/p&gt;

&lt;p&gt;Rahul transfers ₹500 to Priya.&lt;/p&gt;

&lt;p&gt;Two operations are required:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Subtract ₹500 from Rahul's account.&lt;/li&gt;
&lt;li&gt; Add ₹500 to Priya's account.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What happens if the first operation succeeds but the second fails?&lt;/p&gt;

&lt;p&gt;Money would be deducted from Rahul without being added to Priya.&lt;/p&gt;

&lt;p&gt;A transaction helps prevent this partial update.&lt;/p&gt;
&lt;h3&gt;
  
  
  BEGIN
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;BEGIN&lt;/code&gt; starts a transaction.&lt;/p&gt;
&lt;h3&gt;
  
  
  COMMIT
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;COMMIT&lt;/code&gt; saves the transaction's changes permanently.&lt;/p&gt;
&lt;h3&gt;
  
  
  ROLLBACK
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;ROLLBACK&lt;/code&gt; cancels the uncommitted changes made by the current&lt;br&gt;
transaction.&lt;/p&gt;

&lt;p&gt;Let's implement the bank transfer.&lt;/p&gt;

&lt;p&gt;Assume an &lt;code&gt;accounts&lt;/code&gt; table with &lt;code&gt;account_id&lt;/code&gt; and &lt;code&gt;balance&lt;/code&gt; columns&lt;br&gt;
already exists.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;account_id&lt;/span&gt; &lt;span class="o"&gt;=&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;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;account_id&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="k"&gt;COMMIT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If both operations succeed and the transaction commits, the transfer is&lt;br&gt;
saved.&lt;/p&gt;

&lt;p&gt;Now suppose we detect an error before committing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;account_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- An error is detected before the transfer is completed.&lt;/span&gt;

&lt;span class="k"&gt;ROLLBACK&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The uncommitted balance change is undone.&lt;/p&gt;

&lt;p&gt;In a real banking system, we would also validate the account IDs, check&lt;br&gt;
that the sender has enough money, and handle failures correctly. A&lt;br&gt;
database transaction alone does not automatically enforce all business&lt;br&gt;
rules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; &lt;code&gt;ROLLBACK&lt;/code&gt; cannot normally undo a transaction after it&lt;br&gt;
has been committed.&lt;/p&gt;
&lt;h3&gt;
  
  
  ACID properties
&lt;/h3&gt;

&lt;p&gt;Transactions are commonly explained using ACID.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Atomicity:&lt;/strong&gt; Either all required changes happen, or none do.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Consistency:&lt;/strong&gt; The transaction preserves the database's defined
rules and constraints.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Isolation:&lt;/strong&gt; Concurrent transactions are controlled so they do not
interfere in unacceptable ways.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Durability:&lt;/strong&gt; Once committed, changes survive normal failures
covered by the database's durability guarantees.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What I understood:&lt;/strong&gt; Transactions protect related database operations&lt;br&gt;
from being saved only partially. &lt;code&gt;BEGIN&lt;/code&gt; starts the work, &lt;code&gt;COMMIT&lt;/code&gt; saves&lt;br&gt;
it, and &lt;code&gt;ROLLBACK&lt;/code&gt; cancels uncommitted changes.&lt;/p&gt;
&lt;h2&gt;
  
  
  8. Indexes: Primary, Unique, and Composite
&lt;/h2&gt;

&lt;p&gt;An index is a data structure that helps a database find rows without&lt;br&gt;
scanning the entire table in many situations.&lt;/p&gt;

&lt;p&gt;Think of a book's index. Instead of reading every page to find a topic,&lt;br&gt;
you can use the index to locate the relevant pages.&lt;/p&gt;

&lt;p&gt;Database indexes work in a similar way.&lt;/p&gt;

&lt;p&gt;However, indexes also require storage and can make &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt;,&lt;br&gt;
and &lt;code&gt;DELETE&lt;/code&gt; operations more expensive because the indexes may need to&lt;br&gt;
be updated.&lt;/p&gt;
&lt;h3&gt;
  
  
  8.1 Primary Key Index
&lt;/h3&gt;

&lt;p&gt;When we create a primary key, PostgreSQL automatically creates a unique&lt;br&gt;
B-tree index to enforce it, unless the constraint is implemented using&lt;br&gt;
an already-existing suitable index.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;product_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;product_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&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;PostgreSQL uses the primary key's unique index to enforce uniqueness and&lt;br&gt;
help find products by ID.&lt;/p&gt;

&lt;p&gt;We do not need to create another index on &lt;code&gt;product_id&lt;/code&gt; for the same&lt;br&gt;
purpose.&lt;/p&gt;
&lt;h3&gt;
  
  
  8.2 Unique Index
&lt;/h3&gt;

&lt;p&gt;A unique index prevents duplicate indexed key values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_customers_email&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, two customers cannot have the same non-null email.&lt;/p&gt;

&lt;p&gt;In PostgreSQL, a normal unique index allows multiple &lt;code&gt;NULL&lt;/code&gt; values by&lt;br&gt;
default because &lt;code&gt;NULL&lt;/code&gt; values are treated as distinct for uniqueness.&lt;br&gt;
Different options can change this behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difference:&lt;/strong&gt; A primary key is a table constraint that requires&lt;br&gt;
unique, non-null values. A unique index enforces uniqueness, but it does&lt;br&gt;
not by itself make the column &lt;code&gt;NOT NULL&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A table can have only one primary key constraint, but it can have&lt;br&gt;
multiple unique constraints or unique indexes.&lt;/p&gt;
&lt;h3&gt;
  
  
  8.3 Composite Index
&lt;/h3&gt;

&lt;p&gt;A composite index contains more than one column.&lt;/p&gt;

&lt;p&gt;Suppose we frequently retrieve a customer's orders, sorted by date.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_orders_customer_date&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order_date&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This index can help queries such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;order_date&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The order of columns matters.&lt;/p&gt;

&lt;p&gt;An index on &lt;code&gt;(customer_id, order_date)&lt;/code&gt; is generally most useful when a&lt;br&gt;
query filters or sorts using the leading column, &lt;code&gt;customer_id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It can also support searches on both columns, but it is not generally as&lt;br&gt;
useful for filtering only by &lt;code&gt;order_date&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, a query that filters only by &lt;code&gt;order_date&lt;/code&gt; may benefit more&lt;br&gt;
from an index that starts with &lt;code&gt;order_date&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I understood:&lt;/strong&gt; Indexes can speed up data retrieval, but we&lt;br&gt;
should create them based on actual query patterns rather than indexing&lt;br&gt;
every column.&lt;/p&gt;
&lt;h2&gt;
  
  
  9. EXPLAIN: Understanding How SQL Queries Run
&lt;/h2&gt;

&lt;p&gt;We can write a correct SQL query, but that does not mean it will always&lt;br&gt;
run efficiently.&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 sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&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 &lt;code&gt;orders&lt;/code&gt; table contains millions of rows, PostgreSQL needs a way&lt;br&gt;
to find the matching orders.&lt;/p&gt;

&lt;p&gt;It may choose to scan the whole table or use an index.&lt;/p&gt;

&lt;p&gt;How can we understand which approach PostgreSQL chooses?&lt;/p&gt;

&lt;p&gt;We can use &lt;code&gt;EXPLAIN&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PostgreSQL displays the query plan it expects to use.&lt;/p&gt;

&lt;p&gt;Two common plan operations are:&lt;/p&gt;

&lt;h3&gt;
  
  
  Sequential Scan
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Seq Scan on orders
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A sequential scan means PostgreSQL reads the table's rows to find&lt;br&gt;
matching records.&lt;/p&gt;

&lt;p&gt;This is not always bad. If the table is small or the query needs a large&lt;br&gt;
part of the table, scanning it can be the most efficient option.&lt;/p&gt;
&lt;h3&gt;
  
  
  Index Scan
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index Scan using idx_orders_customer_date on orders
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;An index scan means PostgreSQL uses an index to locate relevant rows.&lt;/p&gt;

&lt;p&gt;This can be faster when the query needs only a small portion of a large&lt;br&gt;
table.&lt;/p&gt;

&lt;p&gt;The actual plan depends on the table size, data distribution, available&lt;br&gt;
indexes, and the query itself.&lt;/p&gt;
&lt;h3&gt;
  
  
  EXPLAIN ANALYZE
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;EXPLAIN&lt;/code&gt; shows the planned operations and estimated costs.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; actually executes the query and reports the operations&lt;br&gt;
performed along with execution measurements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;EXPLAIN&lt;/span&gt; &lt;span class="k"&gt;ANALYZE&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It helps us compare estimated and actual row counts and execution time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Be careful:&lt;/strong&gt; &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; executes the query. For a &lt;code&gt;SELECT&lt;/code&gt;, it&lt;br&gt;
runs the read query; for an &lt;code&gt;UPDATE&lt;/code&gt; or &lt;code&gt;DELETE&lt;/code&gt;, it performs the&lt;br&gt;
change. Use caution with write queries, and use a transaction with&lt;br&gt;
&lt;code&gt;ROLLBACK&lt;/code&gt; when appropriate for controlled testing.&lt;/p&gt;

&lt;h3&gt;
  
  
  What should we look at?
&lt;/h3&gt;

&lt;p&gt;When reading a query plan, pay attention to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Scan type:&lt;/strong&gt; Is PostgreSQL using a sequential scan or an index?&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Estimated rows vs.&amp;nbsp;actual rows:&lt;/strong&gt; Are the estimates close to
reality?&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Execution time:&lt;/strong&gt; How long did the query take in this run?&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Buffers:&lt;/strong&gt; With &lt;code&gt;EXPLAIN (ANALYZE, BUFFERS)&lt;/code&gt;, how much data was
read from or found in memory?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do not assume that every sequential scan should be replaced with an&lt;br&gt;
index scan. The goal is to understand why PostgreSQL selected a&lt;br&gt;
particular plan.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I understood:&lt;/strong&gt; &lt;code&gt;EXPLAIN&lt;/code&gt; helps us understand the planned&lt;br&gt;
execution of a query, while &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; lets us compare that plan&lt;br&gt;
with actual execution results.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Putting Everything Together
&lt;/h2&gt;

&lt;p&gt;Let's connect these concepts to a real application.&lt;/p&gt;

&lt;p&gt;Imagine we are developing an online shopping website.&lt;/p&gt;

&lt;p&gt;We need to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Store customer and order information in separate tables.&lt;/li&gt;
&lt;li&gt; Use primary keys to identify records.&lt;/li&gt;
&lt;li&gt; Use foreign keys to connect orders to customers.&lt;/li&gt;
&lt;li&gt; Use joins to retrieve customer details with their orders.&lt;/li&gt;
&lt;li&gt; Use aggregation functions to calculate total sales.&lt;/li&gt;
&lt;li&gt; Normalize tables to avoid unnecessary duplication.&lt;/li&gt;
&lt;li&gt; Use transactions to ensure that related operations succeed or fail
together.&lt;/li&gt;
&lt;li&gt; Create indexes for frequently used search conditions.&lt;/li&gt;
&lt;li&gt; Use &lt;code&gt;EXPLAIN ANALYZE&lt;/code&gt; to investigate slow queries.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Each concept solves a different problem, but all of them work together&lt;br&gt;
to make a database reliable and efficient.&lt;/p&gt;

&lt;p&gt;For example, when a customer places an order, the application might&lt;br&gt;
validate the request, insert the order and its items inside a&lt;br&gt;
transaction, and update inventory. Foreign keys can protect&lt;br&gt;
relationships, constraints can prevent invalid data, and indexes can&lt;br&gt;
help the application find the necessary records efficiently.&lt;/p&gt;

&lt;p&gt;That is why learning database design and query execution is just as&lt;br&gt;
important as learning SQL syntax.&lt;/p&gt;

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

&lt;p&gt;Learning these database concepts helped me understand that databases are&lt;br&gt;
not simply places where we store data. We must also think about how the&lt;br&gt;
data is connected, how it can be retrieved, how duplication can be&lt;br&gt;
avoided, and how changes can be performed safely.&lt;/p&gt;

&lt;p&gt;Here are the key lessons I took away:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Tables and keys&lt;/strong&gt; help us organize and identify data.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Relationships and joins&lt;/strong&gt; help us connect information across
tables.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Aggregations&lt;/strong&gt; help us summarize data.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Normalization (1NF, 2NF, 3NF, BCNF, 4NF, and 5NF)&lt;/strong&gt; helps reduce
unnecessary duplication and dependency problems.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Transactions&lt;/strong&gt; protect related operations.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Indexes&lt;/strong&gt; can improve query performance.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;EXPLAIN&lt;/strong&gt; helps us understand how a database executes queries.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I am continuing to build my understanding of SQL and database design by&lt;br&gt;
practicing these concepts with real examples.&lt;/p&gt;

&lt;p&gt;I hope this article helps other beginners understand these topics more&lt;br&gt;
easily.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What about you?&lt;/strong&gt; Which database concept took you the longest to&lt;br&gt;
understand? Share your experience in the comments!&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>beginners</category>
      <category>fullstack</category>
    </item>
    <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>
