<?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>Forem: MD. JAHID HOSSAIN</title>
    <description>The latest articles on Forem by MD. JAHID HOSSAIN (@jahid6597).</description>
    <link>https://forem.com/jahid6597</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1024179%2F02d4e9eb-6b6f-424a-85cb-c1a462fe53d1.jpg</url>
      <title>Forem: MD. JAHID HOSSAIN</title>
      <link>https://forem.com/jahid6597</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jahid6597"/>
    <language>en</language>
    <item>
      <title>Surviving as a Developer in the Age of AI</title>
      <dc:creator>MD. JAHID HOSSAIN</dc:creator>
      <pubDate>Wed, 01 Apr 2026 17:31:07 +0000</pubDate>
      <link>https://forem.com/jahid6597/surviving-as-a-developer-in-the-age-of-ai-4mig</link>
      <guid>https://forem.com/jahid6597/surviving-as-a-developer-in-the-age-of-ai-4mig</guid>
      <description>&lt;p&gt;Artificial Intelligence isn’t coming for developers - it’s already here. From code generation to automated debugging, AI tools are reshaping how software is built. If you’re feeling a mix of excitement and anxiety, you’re not alone.&lt;/p&gt;

&lt;p&gt;The real question isn’t &lt;em&gt;“Will AI replace developers?”&lt;/em&gt;&lt;br&gt;
It’s &lt;em&gt;“What kind of developer will thrive alongside AI?”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This guide explores that question with real stories, practical advice, workflows, and career strategies to help you thrive in the AI era.&lt;/p&gt;




&lt;h2&gt;
  
  
  📖 A Story: Two Developers, Same Starting Point
&lt;/h2&gt;

&lt;p&gt;Rahim and Arif started their developer careers together.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rahim (The Resistant One)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Avoided AI tools, believing “real developers write everything themselves”&lt;/li&gt;
&lt;li&gt;Spent hours writing boilerplate and debugging manually&lt;/li&gt;
&lt;li&gt;Struggled to meet deadlines&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Arif (The Adaptive One)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Embraced AI for prototyping and debugging&lt;/li&gt;
&lt;li&gt;Reviewed AI-generated code critically&lt;/li&gt;
&lt;li&gt;Focused on architecture, performance, and user experience&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;One year later:&lt;/strong&gt;&lt;br&gt;
Rahim is stuck in manual routines, while Arif leads projects and mentors teams.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lesson:&lt;/strong&gt; Adaptation-not raw talent-determines success in an AI-augmented world.&lt;/p&gt;




&lt;h2&gt;
  
  
  🚀 The Shift: From Writing Code to Solving Problems
&lt;/h2&gt;

&lt;p&gt;Previously, being a great developer meant:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Memorizing syntax&lt;/li&gt;
&lt;li&gt;Writing boilerplate quickly&lt;/li&gt;
&lt;li&gt;Debugging line by line&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, AI can do much of that in seconds. Your value is shifting toward:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem-solving&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;System design&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Understanding user needs&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Making trade-offs&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Code is becoming a &lt;em&gt;commodity&lt;/em&gt;. Thinking is not.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🧠 Learn to Think, Not Just Code
&lt;/h2&gt;

&lt;p&gt;AI can generate code, but it doesn’t truly &lt;em&gt;understand&lt;/em&gt; context. To stay relevant:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Break problems into smaller parts&lt;/li&gt;
&lt;li&gt;Ask precise questions&lt;/li&gt;
&lt;li&gt;Validate AI-generated solutions&lt;/li&gt;
&lt;li&gt;Understand &lt;em&gt;why&lt;/em&gt; something works, not just &lt;em&gt;that&lt;/em&gt; it works&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 Developers who rely blindly on AI plateau. Developers who collaborate with AI accelerate.&lt;/p&gt;




&lt;h2&gt;
  
  
  🛠️ Master the Tools (Before They Replace You)
&lt;/h2&gt;

&lt;p&gt;AI tools are your new teammates. Use them for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Faster prototyping&lt;/li&gt;
&lt;li&gt;AI-assisted debugging&lt;/li&gt;
&lt;li&gt;Writing documentation&lt;/li&gt;
&lt;li&gt;Generating test cases&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Don’t just use them - understand their limitations. When AI fails (and it will), &lt;em&gt;you&lt;/em&gt; are the fallback.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🌍 Real-Life Examples
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Startup MVP in 2 Weeks
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Without AI: Backend 1 week, frontend 1 week, rushed testing&lt;/li&gt;
&lt;li&gt;With AI: Backend scaffolded in 2 days, frontend refined in 3 days, more time for UX testing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Outcome:&lt;/strong&gt; Faster launch, better product quality, stronger market position.&lt;/p&gt;

&lt;h3&gt;
  
  
  Debugging Nightmare
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Developer spent 6 hours on an API bug&lt;/li&gt;
&lt;li&gt;AI-assisted approach found the root cause in 20 minutes, but required human judgment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Lesson:&lt;/strong&gt; AI accelerates work, but fundamentals matter.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Skill Trap
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Junior relied solely on AI → could not explain basic concepts → failed interviews&lt;/li&gt;
&lt;li&gt;Another candidate used AI + understood fundamentals → hired&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Lesson:&lt;/strong&gt; Understanding &amp;gt; output.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔄 Modern AI-Assisted Workflow
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        ┌──────────────────────┐
        │   Define Problem     │
        └─────────┬────────────┘
                  ↓
        ┌──────────────────────┐
        │  Prompt AI Tool      │
        └─────────┬────────────┘
                  ↓
        ┌──────────────────────┐
        │  AI Generates Code   │
        └─────────┬────────────┘
                  ↓
        ┌──────────────────────┐
        │  Human Reviews       │
        │  (Critical Thinking) │
        └─────────┬────────────┘
                  ↓
        ┌──────────────────────┐
        │  Test &amp;amp; Optimize     │
        └─────────┬────────────┘
                  ↓
        ┌──────────────────────┐
        │   Deploy &amp;amp; Monitor   │
        └──────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;AI is part of your workflow - not your replacement.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🧩 Role Evolution
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Before AI&lt;/th&gt;
&lt;th&gt;After AI&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Writing every line&lt;/td&gt;
&lt;td&gt;Reviewing &amp;amp; refining&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Syntax focus&lt;/td&gt;
&lt;td&gt;Logic &amp;amp; architecture&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Manual debugging&lt;/td&gt;
&lt;td&gt;AI-assisted debugging&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Slow iteration&lt;/td&gt;
&lt;td&gt;Rapid experimentation&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;You’re evolving from a &lt;strong&gt;coder&lt;/strong&gt; to an &lt;strong&gt;architect + decision-maker&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🧪 The Hidden Danger: Skill Atrophy
&lt;/h2&gt;

&lt;p&gt;Relying too heavily on AI can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stop you from struggling with problems&lt;/li&gt;
&lt;li&gt;Reduce deep debugging practice&lt;/li&gt;
&lt;li&gt;Prevent learning fundamental concepts&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Solve problems occasionally &lt;em&gt;without&lt;/em&gt; AI&lt;/li&gt;
&lt;li&gt;Write critical logic manually&lt;/li&gt;
&lt;li&gt;Build side projects from scratch&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🧠 Prompting: The New Coding Skill
&lt;/h2&gt;

&lt;p&gt;Bad prompt:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Build a login system”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Good prompt:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Build a secure JWT-based login system with refresh tokens, handle edge cases, and explain trade-offs”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Better prompts reflect clearer thinking, better problem understanding, and improved AI guidance.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔐 Security &amp;amp; Responsibility
&lt;/h2&gt;

&lt;p&gt;AI-generated code can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Introduce vulnerabilities&lt;/li&gt;
&lt;li&gt;Use outdated libraries&lt;/li&gt;
&lt;li&gt;Miss best practices&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;If it breaks, &lt;em&gt;you&lt;/em&gt; are responsible. Always review, test, and validate security.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🤝 Communication: Your New Superpower
&lt;/h2&gt;

&lt;p&gt;AI cannot:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Participate in meetings effectively&lt;/li&gt;
&lt;li&gt;Understand business context&lt;/li&gt;
&lt;li&gt;Make judgment calls&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Developers who can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Explain ideas clearly&lt;/li&gt;
&lt;li&gt;Collaborate across teams&lt;/li&gt;
&lt;li&gt;Translate business requirements into technical solutions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;…will stand out more than ever.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧭 Career Strategy: Specialist vs Generalist
&lt;/h2&gt;

&lt;p&gt;AI excels at general tasks. Developers must choose:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Specialist
&lt;/h3&gt;

&lt;p&gt;Deep expertise in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Security&lt;/li&gt;
&lt;li&gt;Distributed systems&lt;/li&gt;
&lt;li&gt;Machine learning&lt;/li&gt;
&lt;li&gt;Performance optimization&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. High-Level Generalist
&lt;/h3&gt;

&lt;p&gt;Skills include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;System architecture&lt;/li&gt;
&lt;li&gt;Cross-team problem-solving&lt;/li&gt;
&lt;li&gt;Product thinking&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Avoid middle-ground work easily automated by AI.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  ⚡ Speed Matters, Judgment Matters More
&lt;/h2&gt;

&lt;p&gt;AI makes development faster, but speed without judgment leads to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Technical debt&lt;/li&gt;
&lt;li&gt;Security issues&lt;/li&gt;
&lt;li&gt;Poor architecture&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your role evolves into:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Editor, reviewer, and decision-maker - not just a coder.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🎯 AI as a Power Tool
&lt;/h2&gt;

&lt;p&gt;Think of AI like a &lt;strong&gt;power drill&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fast, efficient, and dangerous if misused&lt;/li&gt;
&lt;li&gt;Beginners: build fast but make mistakes faster&lt;/li&gt;
&lt;li&gt;Experts: build better, faster, smarter&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The tool amplifies skill-it doesn’t replace it.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  📈 Career Strategy in the AI Era
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Build a portfolio showing thinking&lt;/strong&gt;&lt;br&gt;
Document trade-offs, decisions, and architectural choices.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Learn in public&lt;/strong&gt;&lt;br&gt;
Share insights, write blogs, and teach concepts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Stay adaptable&lt;/strong&gt;&lt;br&gt;
Advantage comes from how fast you can learn, not what you know today.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  🔮 The Developers Who Will Thrive
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Adapt quickly&lt;/li&gt;
&lt;li&gt;Think critically&lt;/li&gt;
&lt;li&gt;Use AI wisely&lt;/li&gt;
&lt;li&gt;Keep learning fundamentals&lt;/li&gt;
&lt;li&gt;Know when &lt;em&gt;not&lt;/em&gt; to rely on AI&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  💬 Final Thoughts
&lt;/h2&gt;

&lt;p&gt;AI is not the end of developers - it’s the end of &lt;em&gt;average development&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The best developer isn’t the one who writes the most code - it’s the one who creates the most value.&lt;/p&gt;




&lt;h2&gt;
  
  
  🗣️ Discussion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Has AI made you faster-or more dependent?&lt;/li&gt;
&lt;li&gt;Do you trust AI-generated code in production?&lt;/li&gt;
&lt;li&gt;Which skills are you focusing on to stay relevant in 3-5 years?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Drop your thoughts below 👇&lt;/p&gt;

</description>
      <category>ai</category>
      <category>career</category>
      <category>discuss</category>
      <category>programming</category>
    </item>
    <item>
      <title>Tree Shaking in JavaScript: How It Works and Why It Matters</title>
      <dc:creator>MD. JAHID HOSSAIN</dc:creator>
      <pubDate>Wed, 11 Mar 2026 16:50:16 +0000</pubDate>
      <link>https://forem.com/jahid6597/tree-shaking-in-javascript-how-it-works-and-why-it-matters-12be</link>
      <guid>https://forem.com/jahid6597/tree-shaking-in-javascript-how-it-works-and-why-it-matters-12be</guid>
      <description>&lt;p&gt;Modern JavaScript applications often rely on dozens of libraries and utilities.&lt;br&gt;
As projects grow, the bundle size can quickly increase - sometimes including code your application never even uses.&lt;/p&gt;

&lt;p&gt;This unnecessary code slows down loading times and hurts performance.&lt;/p&gt;

&lt;p&gt;Large bundle size is one of the biggest performance problems in modern web applications.&lt;/p&gt;

&lt;p&gt;That’s where &lt;strong&gt;Tree Shaking&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;Tree Shaking is a powerful &lt;strong&gt;JavaScript optimization technique&lt;/strong&gt; used by modern JavaScript bundlers like &lt;strong&gt;Webpack, Rollup, Vite and esbuild&lt;/strong&gt; to automatically remove unused code from your application.&lt;/p&gt;

&lt;p&gt;In this article we will explore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What Tree Shaking is&lt;/li&gt;
&lt;li&gt;Why it matters for performance&lt;/li&gt;
&lt;li&gt;How it works internally&lt;/li&gt;
&lt;li&gt;Practical examples&lt;/li&gt;
&lt;li&gt;Best practices for using it effectively&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  What is Tree Shaking?
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Tree Shaking is a build optimization technique that removes unused JavaScript code from the final bundle.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It works by analyzing &lt;strong&gt;ES module imports and exports&lt;/strong&gt; and eliminating functions, variables, or modules that are not actually used.&lt;/p&gt;
&lt;h3&gt;
  
  
  Simple Explanation
&lt;/h3&gt;

&lt;p&gt;Imagine a file exports 10 functions, but your app only uses 2 of them.&lt;/p&gt;

&lt;p&gt;Tree Shaking ensures that &lt;strong&gt;only those 2 functions remain in the final production bundle&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The other 8 functions are removed automatically during the build process.&lt;/p&gt;


&lt;h2&gt;
  
  
  Why Is It Called “Tree Shaking”?
&lt;/h2&gt;

&lt;p&gt;The name comes from a visual analogy.&lt;/p&gt;

&lt;p&gt;Imagine your codebase as a &lt;strong&gt;tree&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Files → branches&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Functions/exports → leaves&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Imports → connections&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When the bundler analyzes your application, it “shakes the tree” and &lt;strong&gt;unused leaves fall off&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Conceptual Diagram
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            math.js  
   ---------------------------------  
   add | subtract | multiply | divide  
   ---------------------------------  
            |  
            | imported  
            v  
          app.js  
            |  
            v  
     Final Production Bundle  
            |  
            v  
           add()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Unused functions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;subtract()  
multiply()  
divide()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are removed during the build.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Tree Shaking Matters
&lt;/h2&gt;

&lt;p&gt;Without Tree Shaking, your application might ship &lt;strong&gt;a lot of unnecessary code&lt;/strong&gt; to users.&lt;/p&gt;

&lt;p&gt;That leads to slower loading times and worse performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Benefit&lt;/th&gt;
&lt;th&gt;Explanation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Smaller bundle size&lt;/td&gt;
&lt;td&gt;Less JavaScript downloaded&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Faster page load&lt;/td&gt;
&lt;td&gt;Reduced network transfer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Better performance&lt;/td&gt;
&lt;td&gt;Browser parses less code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Improved user experience&lt;/td&gt;
&lt;td&gt;Faster application startup&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lower bandwidth usage&lt;/td&gt;
&lt;td&gt;Especially helpful for mobile users&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Real-Life Example
&lt;/h2&gt;

&lt;p&gt;Imagine you're moving to a new house.&lt;/p&gt;

&lt;p&gt;You open your wardrobe and find:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;clothes you wear every day&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;clothes you haven’t worn for years&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;broken accessories&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;old shoes&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of packing everything, you &lt;strong&gt;only pack what you actually use&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Tree Shaking works the same way.&lt;/p&gt;

&lt;p&gt;Your project may contain many functions, but the final bundle only includes the ones your application needs.&lt;/p&gt;




&lt;h2&gt;
  
  
  Basic Example
&lt;/h2&gt;

&lt;p&gt;Let’s look at a simple example.&lt;/p&gt;

&lt;h3&gt;
  
  
  math.js
&lt;/h3&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="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;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;multiply&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;divide&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;h3&gt;
  
  
  app.js
&lt;/h3&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="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;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;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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the application only uses &lt;code&gt;add()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A tree-shaking-enabled bundler removes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;subtract&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;multiply&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;divide&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;from the final bundle.&lt;/p&gt;




&lt;h2&gt;
  
  
  How Tree Shaking Works
&lt;/h2&gt;

&lt;p&gt;Tree Shaking typically happens during the &lt;strong&gt;build process&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Flow Diagram
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Source Code  
     |  
     v  
Bundler analyzes imports/exports  
     |  
     v  
Builds dependency graph  
     |  
     v  
Detects unused exports  
     |  
     v  
Marks removable code  
     |  
     v  
Optimizer removes unused code  
     |  
     v  
Final optimized bundle
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Build Optimization Pipeline
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Developer Code
      |
      v
Bundler builds dependency graph
      |
      v
Unused exports detected
      |
      v
Tree Shaking removes unused code
      |
      v
Minifier compresses remaining code
      |
      v
Optimized production bundle
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Dependency Graph Visualization
&lt;/h2&gt;

&lt;p&gt;When bundlers analyze code, they create something called a &lt;strong&gt;dependency graph&lt;/strong&gt;.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/  
 ├── main.js  
 ├── utils.js  
 └── api.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Dependency graph:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;         main.js  
        /      \ 
       v        v  
   utils.js    api.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;utils.js&lt;/code&gt; exports multiple functions but &lt;code&gt;main.js&lt;/code&gt; uses only one, Tree Shaking removes the rest.&lt;/p&gt;




&lt;h2&gt;
  
  
  Tree Shaking Works Best With ES Modules
&lt;/h2&gt;

&lt;p&gt;Tree Shaking depends on &lt;strong&gt;static analysis&lt;/strong&gt;, which is easier with &lt;strong&gt;ES modules&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  ES Module Syntax
&lt;/h3&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="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;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="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because imports and exports are static, bundlers can analyze them before execution.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why CommonJS Is Harder to Tree Shake
&lt;/h2&gt;

&lt;p&gt;CommonJS uses dynamic imports.&lt;/p&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;math&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;./math&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="nx"&gt;math&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because &lt;code&gt;require()&lt;/code&gt; can be dynamic, bundlers cannot always determine exactly which functions are used.&lt;/p&gt;

&lt;h3&gt;
  
  
  Comparison
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Module System&lt;/th&gt;
&lt;th&gt;Syntax&lt;/th&gt;
&lt;th&gt;Tree Shaking&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ES Modules&lt;/td&gt;
&lt;td&gt;import/export&lt;/td&gt;
&lt;td&gt;Excellent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CommonJS&lt;/td&gt;
&lt;td&gt;require/module.exports&lt;/td&gt;
&lt;td&gt;Limited&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Real-World Library Example
&lt;/h2&gt;

&lt;p&gt;Let’s look at a simplified example of how Tree Shaking works with a utility library.&lt;/p&gt;

&lt;p&gt;Suppose a utility library exports multiple 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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;debounce&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;throttle&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;memoize&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;deepClone&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;isEqual&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;Your app only imports:&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;debounce&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;./library.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;Without Tree Shaking:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;All 5 functions included
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With Tree Shaking:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Only debounce() included
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example: Tree Shaking With Lodash
&lt;/h2&gt;

&lt;p&gt;A common real-world example of Tree Shaking involves the &lt;strong&gt;Lodash&lt;/strong&gt; utility library.&lt;/p&gt;

&lt;p&gt;Lodash contains many helper functions such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;debounce&lt;/li&gt;
&lt;li&gt;throttle&lt;/li&gt;
&lt;li&gt;cloneDeep&lt;/li&gt;
&lt;li&gt;isEqual&lt;/li&gt;
&lt;li&gt;memoize&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you import Lodash 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;import&lt;/span&gt; &lt;span class="nx"&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;lodash&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;Your bundle may include the entire library, even if you only use one function.&lt;/p&gt;

&lt;p&gt;A better approach is to import only the function you need:&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;debounce&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;lodash/debounce&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;Or using lodash-es, which supports ES modules:&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;debounce&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;lodash-es&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;h2&gt;
  
  
  Tree Shaking vs Dead Code Elimination
&lt;/h2&gt;

&lt;p&gt;These terms are related but slightly different.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Technique&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;Tree Shaking&lt;/td&gt;
&lt;td&gt;Removes unused exports&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dead code elimination&lt;/td&gt;
&lt;td&gt;Removes unreachable code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Minification&lt;/td&gt;
&lt;td&gt;Compresses code&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Tree Shaking focuses on removing unused module exports, while dead code elimination removes code that can never run.&lt;/p&gt;

&lt;p&gt;Example of dead code:&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;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;never runs&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;This code can be removed by optimizers.&lt;/p&gt;




&lt;h2&gt;
  
  
  Side Effects and Tree Shaking
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;side effect&lt;/strong&gt; happens when code runs immediately when a module is imported.&lt;/p&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="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;Module loaded&lt;/span&gt;&lt;span class="dl"&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;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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even if &lt;code&gt;log()&lt;/code&gt; isn't used, the &lt;code&gt;console.log&lt;/code&gt; runs.&lt;/p&gt;

&lt;p&gt;Because of this, bundlers must be careful when removing modules.&lt;/p&gt;

&lt;p&gt;Another common example of side effects is importing CSS:&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles.css&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;Even though nothing is exported from this file, the import has a side effect - it loads styles into the page.&lt;/p&gt;

&lt;p&gt;Because of this, bundlers should not remove this import, even if it appears unused.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pure Module vs Side Effect Module
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Pure module
&lt;/h3&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="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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Safe to remove if unused.&lt;/p&gt;

&lt;h3&gt;
  
  
  Side-effect module
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;loaded&lt;/span&gt;&lt;span class="dl"&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;add&lt;/span&gt;&lt;span class="p"&gt;(){}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;May need to remain.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Patterns That Prevent Tree Shaking
&lt;/h2&gt;

&lt;p&gt;Bad pattern:&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;utils&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
  &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="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="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="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="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="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;utils&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Better pattern:&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="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="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="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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Named exports make Tree Shaking easier.&lt;/p&gt;




&lt;h2&gt;
  
  
  Tree Shaking with Webpack
&lt;/h2&gt;

&lt;p&gt;Webpack supports Tree Shaking in production mode.&lt;/p&gt;

&lt;p&gt;Example package.json:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;  
 &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;  
   &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"webpack --mode production"&lt;/span&gt;&lt;span class="w"&gt;  
 &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;  
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Production builds activate optimizations like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Tree Shaking&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;minification&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;dead code removal&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Using &lt;code&gt;sideEffects&lt;/code&gt; in package.json
&lt;/h2&gt;

&lt;p&gt;Libraries can help bundlers using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;  
 &lt;/span&gt;&lt;span class="nl"&gt;"sideEffects"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="w"&gt;  
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells bundlers that modules do not run code during import, allowing unused files to be safely removed.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real-Life Application Example
&lt;/h2&gt;

&lt;p&gt;Imagine an &lt;strong&gt;e-commerce platform&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Utility functions include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;calculateTax&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;calculateShipping&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;generateInvoicePDF&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;exportSalesReport&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;formatPrice&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The product page only needs:&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;calculateTax&lt;/span&gt;  
&lt;span class="nx"&gt;formatPrice&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tree Shaking ensures the browser &lt;strong&gt;does not download the reporting or PDF generation code&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This improves performance significantly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Tree Shaking in Modern Tools
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;th&gt;Tree Shaking Support&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Webpack&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rollup&lt;/td&gt;
&lt;td&gt;Excellent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Vite&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;esbuild&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Most modern build tools support Tree Shaking by default.&lt;/p&gt;




&lt;h2&gt;
  
  
  How To Enable Tree Shaking
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Tree Shaking usually works automatically when using modern bundlers with ES module syntax in production builds.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Using Webpack
&lt;/h3&gt;

&lt;p&gt;Run the production build:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;webpack &lt;span class="nt"&gt;--mode&lt;/span&gt; production
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Webpack automatically enables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tree Shaking&lt;/li&gt;
&lt;li&gt;Dead code elimination&lt;/li&gt;
&lt;li&gt;Minification&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Using Vite
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Vite uses &lt;strong&gt;Rollup under the hood&lt;/strong&gt;, which performs Tree Shaking automatically.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Rollup
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rollup &lt;span class="nt"&gt;-c&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rollup was originally designed around ES modules, making its Tree Shaking very powerful.&lt;/p&gt;




&lt;h2&gt;
  
  
  Best Practices for Tree Shaking
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Practice&lt;/th&gt;
&lt;th&gt;Why&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Use ES modules&lt;/td&gt;
&lt;td&gt;Easier static analysis&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Prefer named exports&lt;/td&gt;
&lt;td&gt;More precise imports&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Avoid unnecessary side effects&lt;/td&gt;
&lt;td&gt;Enables safe removal&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Import only what you need&lt;/td&gt;
&lt;td&gt;Prevents large bundles&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Use production builds&lt;/td&gt;
&lt;td&gt;Activates optimizations&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Example Project
&lt;/h2&gt;

&lt;h3&gt;
  
  
  utils.js
&lt;/h3&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sum&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="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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;multiply&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="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="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;divide&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  main.js
&lt;/h3&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;sum&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;./utils&lt;/span&gt;&lt;span class="dl"&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;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Production bundle should contain &lt;strong&gt;only the sum function&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final Mental Model
&lt;/h2&gt;

&lt;p&gt;Think of Tree Shaking as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Tree Shaking keeps only the code your application actually uses and removes everything else during the build process.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Your application becomes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;faster&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;lighter&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;more efficient&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;without changing how you write features.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Mistakes That Break Tree Shaking
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Using CommonJS (&lt;code&gt;require&lt;/code&gt;) instead of ES modules&lt;/li&gt;
&lt;li&gt;Importing entire libraries instead of specific functions&lt;/li&gt;
&lt;li&gt;Adding side effects in utility modules&lt;/li&gt;
&lt;li&gt;Converting ES modules to CommonJS with Babel before bundling&lt;/li&gt;
&lt;li&gt;Testing only in development mode instead of production builds&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Tree Shaking is a fundamental optimization technique in modern JavaScript development. As applications grow and dependencies increase, removing unused code becomes critical for performance.&lt;/p&gt;

&lt;p&gt;By structuring your code properly and using ES modules, you allow bundlers to eliminate unnecessary code automatically.&lt;/p&gt;

&lt;p&gt;This leads to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;smaller bundles&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;faster applications&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;better user experiences&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And ultimately, &lt;strong&gt;cleaner production builds&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>discuss</category>
      <category>webdev</category>
      <category>performance</category>
    </item>
    <item>
      <title>Mastering Database Indexing: Faster Data Access Made Simple!</title>
      <dc:creator>MD. JAHID HOSSAIN</dc:creator>
      <pubDate>Wed, 02 Apr 2025 18:36:25 +0000</pubDate>
      <link>https://forem.com/jahid6597/mastering-database-indexing-faster-data-access-made-simple-241k</link>
      <guid>https://forem.com/jahid6597/mastering-database-indexing-faster-data-access-made-simple-241k</guid>
      <description>&lt;p&gt;Imagine you're at a supermarket looking for "chocolate cookies." Would you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check every aisle one by one?&lt;/li&gt;
&lt;li&gt;Use the signs above each aisle to go directly to "Snacks &amp;amp; Cookies"?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4qh4w0rknz5vnggv6rxw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4qh4w0rknz5vnggv6rxw.png" alt="A diagram showing the structure of a B-Tree with internal and leaf nodes." width="800" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Obviously!, you’d use the signs—that’s exactly what &lt;strong&gt;database indexing&lt;/strong&gt; does for your data! Instead of checking every row in a database, indexing helps the system quickly find the information you need. In this blog, we’ll explore the basics of database indexing, its impact on performance, and how it works using simple, real-life examples, code demos, and visuals. Let's dive in!&lt;/p&gt;




&lt;h2&gt;
  
  
  What is Database Indexing?
&lt;/h2&gt;

&lt;p&gt;Imagine a huge database as a giant list of records. Searching through this list without an index is like looking for a needle in a haystack—you’d have to check each record one by one. But with an &lt;strong&gt;index&lt;/strong&gt;, it’s like having a roadmap that shows you exactly where to look!&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;index&lt;/strong&gt; in a database is a special data structure that stores pointers to the actual data in a way that makes retrieving information much faster. It's like a catalog that helps you find what you're looking for without scanning everything.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5vdw4f65w3dwgudh10m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu5vdw4f65w3dwgudh10m.png" alt="A diagram showing the structure of a B+ Tree with only leaf nodes storing data and internal nodes storing keys." width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Real-Life Example: Finding a Customer’s Order
&lt;/h2&gt;

&lt;p&gt;Let’s say you run an e-commerce website, and you need to find a customer’s order. Your database might look like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Order ID&lt;/th&gt;
&lt;th&gt;Customer Name&lt;/th&gt;
&lt;th&gt;Product&lt;/th&gt;
&lt;th&gt;Amount&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;Smartphone&lt;/td&gt;
&lt;td&gt;$500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;Laptop&lt;/td&gt;
&lt;td&gt;$1200&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;Headphones&lt;/td&gt;
&lt;td&gt;$150&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;Tablet&lt;/td&gt;
&lt;td&gt;$300&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;David&lt;/td&gt;
&lt;td&gt;Camera&lt;/td&gt;
&lt;td&gt;$700&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Eve&lt;/td&gt;
&lt;td&gt;Watch&lt;/td&gt;
&lt;td&gt;$200&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;Keyboard&lt;/td&gt;
&lt;td&gt;$100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Frank&lt;/td&gt;
&lt;td&gt;Monitor&lt;/td&gt;
&lt;td&gt;$400&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;Charger&lt;/td&gt;
&lt;td&gt;$50&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;David&lt;/td&gt;
&lt;td&gt;Laptop Stand&lt;/td&gt;
&lt;td&gt;$30&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now, imagine you want to search for "Charlie’s" order. Without an index, the database would scan every row to find "Charlie." This can be slow, especially with a large dataset.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnlluqretjo3apgce25ym.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnlluqretjo3apgce25ym.png" alt="A comparison chart showing the differences between B-Tree and B+ Tree." width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But if we create an &lt;strong&gt;index&lt;/strong&gt; on the &lt;code&gt;Customer Name&lt;/code&gt; column, the database can quickly find "Charlie" without checking every row.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sorted Reference Table Example
&lt;/h3&gt;

&lt;p&gt;The index on &lt;code&gt;Customer Name&lt;/code&gt; might look like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Customer Name&lt;/th&gt;
&lt;th&gt;Order ID&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;3, 4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;1, 7&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;2, 9&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;David&lt;/td&gt;
&lt;td&gt;5, 10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Eve&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Frank&lt;/td&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now, instead of scanning all records, the database looks up "Charlie" in the index and directly fetches his order IDs, making the query much faster!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F938fqoggs4tltp1m9i4j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F938fqoggs4tltp1m9i4j.png" alt="A flowchart showing the steps involved in creating and balancing a B-Tree." width="800" height="138"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Types of Indexing Structures: B-Tree and B+ Tree
&lt;/h2&gt;

&lt;p&gt;Modern databases use advanced indexing structures like &lt;strong&gt;B-Trees&lt;/strong&gt; and &lt;strong&gt;B+ Trees&lt;/strong&gt; to organize data efficiently.&lt;/p&gt;

&lt;h3&gt;
  
  
  B-Tree Index
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;B-Tree&lt;/strong&gt; (Balanced Tree) stores both &lt;strong&gt;keys&lt;/strong&gt; (values being indexed) and &lt;strong&gt;data&lt;/strong&gt; in its nodes. The tree is balanced, which means that all leaf nodes are at the same depth, ensuring quick access.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Example&lt;/strong&gt;: Searching for "Charlie" in a B-Tree means traversing a few nodes until you find the data.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        Bob
       /   \
   Alice   Charlie
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  B+ Tree Index
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;B+ Tree&lt;/strong&gt; is similar to the B-Tree, but it stores &lt;strong&gt;only keys&lt;/strong&gt; in the internal nodes, while all data is kept in the leaf nodes. The leaf nodes are also linked, making range queries much more efficient.&lt;/p&gt;

&lt;p&gt;Example: With a B+ Tree, finding all customers whose names start with "C" is quick because all the relevant data is stored in linked leaf nodes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        Bob
       /   \
   Alice   Charlie --&amp;gt; Leaf Node
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key difference between B-Trees and B+ Trees is that B+ Trees allow for faster range queries, making them the preferred choice in many modern databases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Full Tree Example with More Complexity
&lt;/h2&gt;

&lt;p&gt;Let’s dive deeper into a more complex example with a &lt;strong&gt;B-Tree&lt;/strong&gt; and &lt;strong&gt;B+ Tree&lt;/strong&gt;, both of which are commonly used for indexing.&lt;/p&gt;

&lt;h3&gt;
  
  
  B-Tree Example
&lt;/h3&gt;

&lt;p&gt;Consider the following dataset of customer names and their order IDs:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Customer Name&lt;/th&gt;
&lt;th&gt;Order ID&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;David&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Eve&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Frank&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Grace&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;We’ll create a &lt;strong&gt;B-Tree&lt;/strong&gt; index for the &lt;code&gt;Customer Name&lt;/code&gt; field.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Insert 'Alice'&lt;/strong&gt;: Alice is placed at the root.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Insert 'Bob'&lt;/strong&gt;: Bob is smaller than Alice, so it goes to the left.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Insert 'Charlie'&lt;/strong&gt;: Charlie is placed on the right of Alice.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Insert 'David'&lt;/strong&gt;: David fits in between Charlie and Eve, so it goes to the right.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Insert 'Eve'&lt;/strong&gt;: Eve fits between David and Frank, so it goes on the right side.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s how the B-Tree would look after inserting all elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            Alice
          /       \
      Bob          Charlie
                     /      \
                 David      Eve
                             /   \
                          Frank  Grace
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this tree, you can see that if you want to search for "David", you start at the root, then go to the right subtree, and finally find the node.&lt;/p&gt;

&lt;h3&gt;
  
  
  B+ Tree Example
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;B+ Tree&lt;/strong&gt; is more efficient for range queries. In a &lt;strong&gt;B+ Tree&lt;/strong&gt;, only leaf nodes contain data, and the internal nodes contain keys. All leaf nodes are also &lt;strong&gt;linked&lt;/strong&gt;, making range queries much more efficient.&lt;/p&gt;

&lt;p&gt;Let’s use the same dataset for a &lt;strong&gt;B+ Tree&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Customer Name&lt;/th&gt;
&lt;th&gt;Order ID&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;David&lt;/td&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Eve&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Frank&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Grace&lt;/td&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The B+ Tree would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;         [Charlie]
        /         \
    [Bob]        [David]
       |            |   
    [Alice]      [Eve] --&amp;gt; Linked to Next Leaf
                   /   \
               [Frank] [Grace]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this structure, only leaf nodes store the actual data, while internal nodes only store the keys. Notice how the leaf nodes are linked, allowing you to easily perform range queries (e.g., find all orders from "Charlie" to "Grace").&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Differences Between B-Tree and B+ Tree
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Feature&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;B-Tree&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;B+ Tree&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Data Storage&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Data in both internal and leaf nodes&lt;/td&gt;
&lt;td&gt;Data only in leaf nodes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Internal Nodes&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Store both keys and data&lt;/td&gt;
&lt;td&gt;Store only keys&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Leaf Node Linking&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes, linked leaf nodes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Best Use Case&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Point queries&lt;/td&gt;
&lt;td&gt;Range queries, large data sets&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Impact of Indexing on Read vs. Write Operations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Read Operations (Queries)
&lt;/h3&gt;

&lt;p&gt;Indexes are designed to speed up &lt;strong&gt;read operations&lt;/strong&gt;. When you query the database, it uses the index to jump directly to the relevant data, saving time.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Without Index&lt;/strong&gt;: Searching for "Charlie" would require scanning every row in the table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;With Index&lt;/strong&gt;: The database looks up "Charlie" in the index, making the query faster.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, if you're searching for a customer by name in a large table, without an index, the database needs to check every row, which can be slow. But with an index, the search is much faster as it directly finds the location of the data in a sorted structure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0of94ikvut7klf7nhwwn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0of94ikvut7klf7nhwwn.png" alt="A diagram illustrating the difference between B-Tree and B+ Tree, highlighting the data storage and key structures." width="800" height="890"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Write Operations (Insert, Update, Delete)
&lt;/h3&gt;

&lt;p&gt;While indexes improve read performance, they can &lt;strong&gt;slow down write operations&lt;/strong&gt;. When a new row is inserted, updated, or deleted, the index must also be updated to reflect the changes in the data.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Insert&lt;/strong&gt;: When a new row is inserted, the database not only inserts the row into the table but also updates the index, which takes extra time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Update&lt;/strong&gt;: If an indexed column is updated, the index must be modified to reflect the new value, which adds overhead to the update process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delete&lt;/strong&gt;: When a row is deleted, the corresponding entry in the index must also be removed, adding extra processing time.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In summary, while indexes significantly speed up read queries, they add overhead to write operations, especially in tables with multiple indexes or large datasets.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs0l643jpfu8lk4zvj53w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs0l643jpfu8lk4zvj53w.png" alt="A table comparing B-Tree and B+ Tree with key differences in data storage, internal nodes, and leaf node linking." width="800" height="687"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Example: Indexing in SQL (With Detailed Analysis)
&lt;/h2&gt;

&lt;p&gt;Indexes significantly improve query performance by allowing the database to find records quickly instead of scanning the entire table. However, they come with trade-offs, especially for write operations.&lt;/p&gt;

&lt;p&gt;In this section, we will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a table&lt;/li&gt;
&lt;li&gt;Insert sample data&lt;/li&gt;
&lt;li&gt;Compare queries with and without an index&lt;/li&gt;
&lt;li&gt;Analyze query execution plans&lt;/li&gt;
&lt;li&gt;Discuss the impact of indexes on different operations&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  1. Creating a Sample Table
&lt;/h2&gt;

&lt;p&gt;Let's create a &lt;code&gt;customers&lt;/code&gt; table with a few columns.&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;id&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&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;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;city&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;50&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;ul&gt;
&lt;li&gt;
&lt;code&gt;id&lt;/code&gt;: Primary key (automatically indexed).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt;: A column we may index later.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;email&lt;/code&gt;: Unique constraint (automatically indexed).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;city&lt;/code&gt;: A regular column.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Inserting Sample Data
&lt;/h2&gt;

&lt;p&gt;To make the performance impact visible, let's insert &lt;strong&gt;1 million&lt;/strong&gt; records.&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;DO&lt;/span&gt; &lt;span class="err"&gt;$$&lt;/span&gt; 
&lt;span class="k"&gt;BEGIN&lt;/span&gt; 
    &lt;span class="k"&gt;FOR&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;1000000&lt;/span&gt; &lt;span class="n"&gt;LOOP&lt;/span&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;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="n"&gt;city&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="s1"&gt;'Customer'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'customer'&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s1"&gt;'@example.com'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="k"&gt;CASE&lt;/span&gt; &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="s1"&gt;'New York'&lt;/span&gt;
                 &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&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;THEN&lt;/span&gt; &lt;span class="s1"&gt;'Los Angeles'&lt;/span&gt;
                 &lt;span class="k"&gt;ELSE&lt;/span&gt; &lt;span class="s1"&gt;'Chicago'&lt;/span&gt;
            &lt;span class="k"&gt;END&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;END&lt;/span&gt; &lt;span class="n"&gt;LOOP&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;END&lt;/span&gt; &lt;span class="err"&gt;$$&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;This Script:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inserts &lt;strong&gt;1 million rows&lt;/strong&gt; into the &lt;code&gt;customers&lt;/code&gt; table.&lt;/li&gt;
&lt;li&gt;Generates unique names (&lt;code&gt;Customer1&lt;/code&gt;, &lt;code&gt;Customer2&lt;/code&gt;, ...).&lt;/li&gt;
&lt;li&gt;Ensures unique emails (&lt;code&gt;customer1@example.com&lt;/code&gt;, &lt;code&gt;customer2@example.com&lt;/code&gt;, ...).&lt;/li&gt;
&lt;li&gt;Randomly assigns a city (&lt;code&gt;New York&lt;/code&gt;, &lt;code&gt;Los Angeles&lt;/code&gt;, &lt;code&gt;Chicago&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Query Without an Index
&lt;/h2&gt;

&lt;p&gt;Let's search for a customer &lt;strong&gt;without an index&lt;/strong&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;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;customers&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Customer500000'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Expected Output (Without Index)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Seq Scan on customers  (cost=0.00..43125.00 rows=1 width=68)
   Filter: (name = 'Customer500000'::text)
   Rows Removed by Filter: 999999
   Planning Time: 0.125 ms
   Execution Time: 2034.45 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Seq Scan → Scans every row.&lt;/li&gt;
&lt;li&gt;Rows Removed → Checks all 1 million rows.&lt;/li&gt;
&lt;li&gt;Execution Time → ~2 seconds for one query.
This is highly inefficient.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Creating an Index
&lt;/h2&gt;

&lt;p&gt;Let's add an index on the name column.&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_customer_name&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;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, rerun the query:&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;customers&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Customer500000'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Expected Output (With Index)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index Scan using idx_customer_name on customers  (cost=0.42..8.44 rows=1 width=68)
   Index Cond: (name = 'Customer500000'::text)
   Planning Time: 0.095 ms
   Execution Time: 0.135 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What Improved?&lt;/strong&gt;&lt;br&gt;
✅ Index Scan → Uses the index instead of scanning all rows.&lt;br&gt;
✅ Execution Time → Reduced from 2 seconds to 0.1 ms → HUGE improvement!&lt;/p&gt;
&lt;h2&gt;
  
  
  5. Impact on Insert, Update, and Delete
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;5.1 Insert Performance&lt;/strong&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;ANALYZE&lt;/span&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;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="n"&gt;city&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="s1"&gt;'NewCustomer'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'newcustomer@example.com'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Boston'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Expected Output (With Index)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Insert on customers  (cost=0.00..0.01 rows=1 width=0)
   Execution Time: 0.455 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Inserts are slightly slower because the database updates the index.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5.2 Update Performance&lt;/strong&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;ANALYZE&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'San Francisco'&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Customer500000'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Expected Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index Scan using idx_customer_name on customers  (cost=0.42..8.44 rows=1 width=68)
   Index Cond: (name = 'Customer500000'::text)
   Execution Time: 0.225 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Finding the record is fast due to the index.&lt;/li&gt;
&lt;li&gt;If updating an indexed column, the database rebuilds the index, making it slower.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5.3 Delete Performance&lt;/strong&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;ANALYZE&lt;/span&gt; &lt;span class="k"&gt;DELETE&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;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Customer500000'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Expected Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Index Scan using idx_customer_name on customers  (cost=0.42..8.44 rows=1 width=68)
   Index Cond: (name = 'Customer500000'::text)
   Execution Time: 0.290 ms
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Fast lookup, but removing from index adds overhead.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. Dropping an Index
&lt;/h2&gt;

&lt;p&gt;If indexing slows down inserts/updates, remove it.&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;DROP&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_customer_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After dropping the index:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Searches become slower.&lt;/li&gt;
&lt;li&gt;Inserts, updates, and deletes become faster.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7. When to Use Indexes?
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;Use an Index?&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Frequently searching a column&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sorting a large dataset&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Joining large tables&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Frequently updating a column&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;High insert/delete operations&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Best Practices&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Index columns used in WHERE, ORDER BY, and JOIN.&lt;/li&gt;
&lt;li&gt;Avoid indexing frequently updated columns.&lt;/li&gt;
&lt;li&gt;Limit the number of indexes.&lt;/li&gt;
&lt;li&gt;Use Composite Indexes if filtering by multiple columns.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example of a Composite 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;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_name_city&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;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;city&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, searches on both &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;city&lt;/code&gt; are optimized.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Summary
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Operation&lt;/th&gt;
&lt;th&gt;Without Index&lt;/th&gt;
&lt;th&gt;With Index&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SELECT&lt;/td&gt;
&lt;td&gt;Slow (Full Scan)&lt;/td&gt;
&lt;td&gt;Fast (Index Scan)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;INSERT&lt;/td&gt;
&lt;td&gt;Fast&lt;/td&gt;
&lt;td&gt;Slightly Slower&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UPDATE (Indexed Column)&lt;/td&gt;
&lt;td&gt;Fast&lt;/td&gt;
&lt;td&gt;Slower (Index Must Update)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;DELETE&lt;/td&gt;
&lt;td&gt;Fast&lt;/td&gt;
&lt;td&gt;Slightly Slower&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Indexes boost query performance but come at the cost of slower writes. The key is balancing speed vs. overhead.&lt;/p&gt;




&lt;h2&gt;
  
  
  Types of Indexes (Other than B-Tree/B+ Tree)
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Hash Indexing
&lt;/h3&gt;

&lt;p&gt;A hash index is an indexing technique that uses a hash table. It's often used for equality comparisons (&lt;code&gt;=&lt;/code&gt;) rather than range queries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
If you're looking for a record where the value of a column equals a specific value, hash indexing can be very efficient:&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_hash_email&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="n"&gt;HASH&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;However, hash indexing is not useful for range queries (&lt;code&gt;BETWEEN&lt;/code&gt;, &lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;&lt;/code&gt;), and some databases (like MySQL) do not support hash indexes for general use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Full-Text Indexing
&lt;/h3&gt;

&lt;p&gt;Full-text indexing is used to optimize search queries on large text fields. In full-text search, the text is tokenized, and each token is indexed separately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example (MySQL):&lt;/strong&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;CREATE&lt;/span&gt; &lt;span class="n"&gt;FULLTEXT&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_fulltext_desc&lt;/span&gt; &lt;span class="k"&gt;ON&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;description&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would optimize queries like:&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;products&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;MATCH&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;AGAINST&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'laptop'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Spatial Indexing
&lt;/h3&gt;

&lt;p&gt;Spatial indexing is used to optimize queries on geometric or geographic data, like points, lines, or polygons.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example (PostgreSQL with PostGIS):&lt;/strong&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;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_gist_location&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;locations&lt;/span&gt; &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="n"&gt;GIST&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;geom&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Indexing Strategies
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Composite Indexes
&lt;/h3&gt;

&lt;p&gt;A composite index is an index on multiple columns, which is helpful when queries filter by multiple columns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&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;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_composite_name_age&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;users&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;age&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 will optimize queries like:&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;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'John'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Partial Indexes
&lt;/h3&gt;

&lt;p&gt;Partial indexes index only a subset of the data, which can be useful for reducing index size and increasing performance for specific queries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&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;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_partial_active_users&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;users&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="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;is_active&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;This index would be useful for queries like:&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;users&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;is_active&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;AND&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'John'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Unique Indexes
&lt;/h3&gt;

&lt;p&gt;A unique index ensures that all the values in the indexed columns are distinct.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&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;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_unique_email&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;users&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;This would enforce that no two users can have the same email address.&lt;/p&gt;




&lt;h2&gt;
  
  
  Index Maintenance
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Index Fragmentation
&lt;/h3&gt;

&lt;p&gt;Indexes can become fragmented as rows are added, updated, or deleted. Fragmentation can reduce performance, and indexes might need to be rebuilt periodically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In PostgreSQL:&lt;/strong&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;REINDEX&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In MySQL:&lt;/strong&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="n"&gt;OPTIMIZE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Rebuilding and Dropping Indexes
&lt;/h3&gt;

&lt;p&gt;Indexes might need to be rebuilt or dropped when they are no longer needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example (Rebuild Index):&lt;/strong&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;ALTER&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_name&lt;/span&gt; &lt;span class="n"&gt;REBUILD&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Performance Implications
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Query Optimization
&lt;/h3&gt;

&lt;p&gt;Indexes help the database engine find rows more efficiently. The optimizer decides whether to use an index based on factors like query complexity, index statistics, and available indexes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
A query without an index might require scanning the entire table (a full table scan), while an indexed query will only look at the rows that match the index criteria.&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;products&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;category&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'electronics'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If there's an index on &lt;code&gt;category&lt;/code&gt;, the database can jump directly to the relevant rows.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cost of Indexing
&lt;/h3&gt;

&lt;p&gt;While indexes speed up read operations, they slow down write operations because the index must be updated every time the table data changes. The trade-off must be considered based on the application’s needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
If a table frequently experiences &lt;code&gt;INSERT&lt;/code&gt;, &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; operations, adding too many indexes can degrade performance due to the extra overhead in maintaining those indexes.&lt;/p&gt;


&lt;h2&gt;
  
  
  Covering Indexes
&lt;/h2&gt;

&lt;p&gt;A covering index is an index that contains all the columns needed for a query, so the database can satisfy the query using only the index (without needing to access the table).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&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;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_covering&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;order_id&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;total_amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a query like:&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;order_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;total_amount&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;123&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;idx_covering&lt;/code&gt; index includes both &lt;code&gt;order_id&lt;/code&gt;, &lt;code&gt;customer_id&lt;/code&gt; and &lt;code&gt;total_amount&lt;/code&gt; the query can be satisfied directly by the index.&lt;/p&gt;




&lt;h2&gt;
  
  
  Database-Specific Indexing Features
&lt;/h2&gt;

&lt;h3&gt;
  
  
  PostgreSQL
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;GiST Indexes (Generalized Search Tree): A versatile indexing method used for various data types like geometric and full-text data.
&lt;/li&gt;
&lt;/ul&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_gist_geom&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;places&lt;/span&gt; &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="n"&gt;GiST&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;location&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;GIN Indexes (Generalized Inverted Index): Useful for full-text search and array data.
&lt;/li&gt;
&lt;/ul&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_gin_tags&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;posts&lt;/span&gt; &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="n"&gt;GIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  MySQL
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;InnoDB vs. MyISAM: InnoDB uses clustered indexes (the table data is stored in the index), while MyISAM uses non-clustered indexes.
&lt;/li&gt;
&lt;/ul&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_name_age&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;users&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;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;USING&lt;/span&gt; &lt;span class="n"&gt;BTREE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  SQL Server
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Indexed Views: A feature in SQL Server where views can be indexed for performance optimization.
&lt;/li&gt;
&lt;/ul&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;VIEW&lt;/span&gt; &lt;span class="n"&gt;vw_sales_summary&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
  &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;product_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;sales_amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sales&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;product_id&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;UNIQUE&lt;/span&gt; &lt;span class="n"&gt;CLUSTERED&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_sales_summary&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;vw_sales_summary&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;product_id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Indexing and Normalization
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Normalization
&lt;/h3&gt;

&lt;p&gt;In normalized databases, indexes optimize queries by providing fast access paths to specific data points, such as foreign key relationships.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
In a normalized schema, an index on foreign keys improves join performance.&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_fk_user_id&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;user_id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Denormalization
&lt;/h3&gt;

&lt;p&gt;In denormalized databases (where data is repeated across multiple tables), indexes are still crucial, especially on columns that are frequently queried for joins or filters.&lt;/p&gt;




&lt;h2&gt;
  
  
  Indexes in NoSQL Databases
&lt;/h2&gt;

&lt;h3&gt;
  
  
  MongoDB Indexing
&lt;/h3&gt;

&lt;p&gt;MongoDB supports various index types, including compound and geospatial indexes.&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;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createIndex&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="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;&lt;strong&gt;For geospatial data:&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="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;places&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createIndex&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;location&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2dsphere&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;h2&gt;
  
  
  Elasticsearch
&lt;/h2&gt;

&lt;p&gt;Elasticsearch uses inverted indexing to optimize full-text search across large datasets.&lt;/p&gt;




&lt;h2&gt;
  
  
  Advanced Topics in Indexing
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Multi-Level Indexing
&lt;/h3&gt;

&lt;p&gt;Some databases use multi-level indexing to optimize the search process by splitting the data into multiple levels of indexes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bitmap Indexing
&lt;/h3&gt;

&lt;p&gt;Bitmap indexing is used for columns with low cardinality, such as boolean or categorical 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;INDEX&lt;/span&gt; &lt;span class="n"&gt;idx_bitmap&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_active&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adaptive Indexing
&lt;/h3&gt;

&lt;p&gt;Adaptive indexing dynamically changes the index structure based on query load.&lt;/p&gt;




&lt;h2&gt;
  
  
  When Should You Use Indexes?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Read-heavy Workloads: If your database often queries specific columns, indexes can make searches faster.&lt;/li&gt;
&lt;li&gt;Sorting and Range Queries: Indexes help when you need to sort or filter large datasets.&lt;/li&gt;
&lt;li&gt;Unique Data: Columns with unique values (like user email addresses or order IDs) benefit from indexing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When Should You Avoid Indexes?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Write-heavy Workloads: If your application constantly inserts, updates, or deletes data, too many indexes can slow things down.&lt;/li&gt;
&lt;li&gt;Small Tables: If your table has very few records, scanning the whole table might be faster than using an index.&lt;/li&gt;
&lt;li&gt;Low Selectivity Columns: Columns with repetitive values (e.g., gender) don’t benefit much from indexing.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Key Takeaways
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Indexes help speed up data retrieval by providing a faster way to find records.&lt;/li&gt;
&lt;li&gt;B-Trees and B+ Trees are common indexing structures that make searches faster and more efficient.&lt;/li&gt;
&lt;li&gt;Indexes speed up read operations but can slow down write operations.&lt;/li&gt;
&lt;li&gt;Use indexes wisely to optimize your database performance, especially for read-heavy applications!&lt;/li&gt;
&lt;/ul&gt;




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

&lt;ul&gt;
&lt;li&gt;Indexing helps speed up read operations by providing quick access to data.&lt;/li&gt;
&lt;li&gt;It can slow down write operations because the index also needs to be updated during inserts, updates, and deletes.&lt;/li&gt;
&lt;li&gt;B-Tree and B+ Tree are commonly used indexing structures, with B+ Tree being more efficient for range queries.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By understanding the benefits and trade-offs of indexing, you can improve database performance and make your applications more efficient!&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>database</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>JavaScript: 25 Weird Moments</title>
      <dc:creator>MD. JAHID HOSSAIN</dc:creator>
      <pubDate>Wed, 10 Apr 2024 16:41:22 +0000</pubDate>
      <link>https://forem.com/jahid6597/unleashing-the-quirky-and-weird-a-dive-into-the-world-of-javascript-22al</link>
      <guid>https://forem.com/jahid6597/unleashing-the-quirky-and-weird-a-dive-into-the-world-of-javascript-22al</guid>
      <description>&lt;p&gt;JavaScript, the language of the web, is not just a tool for creating interactive websites and web applications; it's also a playground for the quirky and the weird. In this blog post, we'll explore some of the funniest and most bizarre aspects of JavaScript, accompanied by real-life code examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;1. NaN is Weird&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;NaN, short for "Not a Number," is a peculiar beast in JavaScript. It represents a value that is not a legal number. But here's the kicker – NaN is not equal to itself!&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;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="kc"&gt;NaN&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;NaN&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This seemingly nonsensical behavior can lead to unexpected results if you're not careful.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;2. The Falsy-Truthy Circus&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript's loose equality rules can lead to some unexpected outcomes, especially when dealing with falsy and truthy values. 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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&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="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&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="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="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's like a circus where 1 can equal true, but 2 doesn't make the cut!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;3. The Curious Case of Coercion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript's automatic type coercion can sometimes lead to bizarre results. Consider the following:&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// "12"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&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="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 0.5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript tries to be helpful by converting types implicitly, but it can also cause headaches if you're not aware of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;4. The Infamous Callback Hell&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript's asynchronous nature can lead to what developers affectionately call "callback hell." Imagine a scenario where you have multiple nested callbacks, each triggering the next one.&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;doSomething&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="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;doSomethingElse&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;doYetAnotherThing&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="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// And it goes on...&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;It's like a labyrinth of callbacks, making your code hard to read and maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;5. The Strange Case of "this"&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The behavior of the this keyword in JavaScript can be perplexing, especially for newcomers. Its value depends on how a function is called, which can lead to unexpected results.&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;foo&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="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="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;obj&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="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value of &lt;code&gt;this&lt;/code&gt; can change depending on the context of the function call, leading to confusion and frustration.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;6. The Mysterious Closure&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Closures in JavaScript are powerful, but they can also be a source of confusion for many developers. Consider the following 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;outer&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;inner&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="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;count&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;inner&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;closureFunc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;outer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;closureFunc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="nf"&gt;closureFunc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though count is declared within the &lt;code&gt;outer&lt;/code&gt; function and seemingly out of scope once &lt;code&gt;outer&lt;/code&gt; finishes executing, the &lt;code&gt;inner&lt;/code&gt; function still retains access to it. This phenomenon, known as closure, can be mind-bending for those encountering it for the first time.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;7. The Elusive '==='&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;While JavaScript's loose equality operator (==) can lead to unexpected results due to type coercion, its strict equality operator (===) is supposed to be more reliable. However, even === can have its quirks:&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;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="kc"&gt;NaN&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;NaN&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: false&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="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;===&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="c1"&gt;// Output: true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Comparing NaN with NaN using strict equality returns false, which is consistent with its behavior elsewhere. But comparing positive zero with negative zero using === yields true, which might not be intuitive for many developers.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;8. The Surreal World of Hoisting&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript's variable and function declarations are hoisted to the top of their containing scope during compilation, which can lead to some unexpected behavior:&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: undefined&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: "Hello, world!"&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;sayHello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, world!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though x is logged before it's declared and initialized, it doesn't result in an error; instead, it outputs undefined. Similarly, the sayHello function can be called before its declaration in the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;9. The Perplexing 'typeof' Operator&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The typeof operator in JavaScript is used to determine the data type of its operand. However, it can yield some puzzling results:&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;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="k"&gt;typeof&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: "object"&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="k"&gt;typeof&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt; &lt;span class="c1"&gt;// Output: "object"&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="k"&gt;typeof&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{});&lt;/span&gt; &lt;span class="c1"&gt;// Output: "function"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The fact that &lt;code&gt;typeof null&lt;/code&gt; returns &lt;code&gt;"object"&lt;/code&gt; rather than &lt;code&gt;"null"&lt;/code&gt; can catch many developers off guard. Additionally, both arrays (&lt;code&gt;[]&lt;/code&gt;) and functions (&lt;code&gt;function() {}&lt;/code&gt;) are considered to be of type &lt;code&gt;"object"&lt;/code&gt; by the &lt;code&gt;typeof&lt;/code&gt; operator.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;10. The Whimsical 'for...in' Loop&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;for...in&lt;/code&gt; loop in JavaScript is used to iterate over the enumerable properties of an object. However, it can also traverse unexpected properties inherited from the object's prototype chain:&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;person&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;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sayHello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello!&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;for &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;prop&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;person&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;prop&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: "name", "age", "sayHello"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;for...in&lt;/code&gt; loop not only iterates over the name and age properties of the person object but also includes the sayHello method inherited from &lt;code&gt;Object.prototype&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;11. The Bewildering 'eval()' Function&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript's &lt;code&gt;eval()&lt;/code&gt; function can execute JavaScript code represented as a string:&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;y&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;eval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x + y&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="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While &lt;code&gt;eval()&lt;/code&gt; can be powerful, it's often considered a security risk and can lead to code that's challenging to debug and maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;12. The Mysterious 'void' Operator&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;void&lt;/code&gt; operator evaluates an expression and then returns &lt;code&gt;undefined&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a niche operator mostly used to obtain the undefined primitive value, typically in scenarios where a value is not needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;13. The Uncanny 'with' Statement&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The with statement extends the scope chain for a statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = { x: 10 };
with (obj) {
  console.log(x); // Output: 10
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While &lt;code&gt;with&lt;/code&gt; can lead to concise code, it's generally discouraged due to performance implications and potential confusion about variable scopes.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;14. The Cryptic 'delete' Operator&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The delete operator removes a property from an object:&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;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;x&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="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;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;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, it doesn't always work as expected, especially with properties inherited through the prototype chain.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;15. The Strange 'arguments' Object&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Inside functions, the arguments object provides access to all arguments passed to the 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;function&lt;/span&gt; &lt;span class="nf"&gt;sum&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;total&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;for &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;i&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="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&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="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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;total&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="nf"&gt;sum&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="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="c1"&gt;// Output: 6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While &lt;code&gt;arguments&lt;/code&gt; can be handy for functions with variable numbers of arguments, it's considered a bit old-fashioned, with the advent of rest parameters (&lt;code&gt;...args&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;16. The Esoteric 'Generator Functions'&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Generator functions allow you to define an iterative algorithm by writing a single function with multiple yield expressions:&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;generateSequence&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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sequence&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generateSequence&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;sequence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&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;span class="c1"&gt;// Output: 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="nx"&gt;sequence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&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;span class="c1"&gt;// Output: 2&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;sequence&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&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;span class="c1"&gt;// Output: 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generator functions can be paused and resumed, offering a powerful mechanism for creating iterators.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;17. The Enigmatic 'Proxy' Object&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The Proxy object enables you to create a proxy for another object, allowing you to intercept and customize 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&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;handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;get&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;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prop&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;prop&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;]&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="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;proxy&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;Proxy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&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;proxy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Proxies can be used for a variety of tasks, including logging, validation, and more.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;18. The Eccentric 'Array Methods'&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript arrays come with a plethora of methods for manipulation and traversal, such as &lt;code&gt;map()&lt;/code&gt;, &lt;code&gt;filter()&lt;/code&gt;, and &lt;code&gt;reduce()&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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="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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doubled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&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="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;doubled&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: [2, 4, 6, 8, 10]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Array methods offer a functional programming style, enabling concise and expressive code.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;19. The Curious 'Symbol.species' Property&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;Symbol.species&lt;/code&gt; property allows you to customize the constructor function used to create derived objects:&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;MyArray&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Array&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;species&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="nb"&gt;Array&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;myArray&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;MyArray&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="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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mapped&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;x&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;mapped&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;MyArray&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: false&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;mapped&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Symbol.species&lt;/code&gt; can be handy for ensuring consistent behavior across subclass instances.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;20. The Intriguing 'Intl' Object&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;Intl&lt;/code&gt; object provides language-sensitive functionalities for internationalization and localization:&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;date&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;Date&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;formatted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Intl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DateTimeFormat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;en-US&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;date&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;formatted&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: "4/10/2024"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From date and time formatting to number formatting and collation, Intl offers robust support for handling internationalization tasks.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;21. The Bizarre 'Falsy' Behavior of Empty Arrays&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Empty arrays in JavaScript are considered truthy, contrary to what one might expect:&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;if &lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;An empty array is truthy!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: "An empty array is truthy!"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While arrays with elements evaluate as truthy, an empty array is also considered truthy, which can be surprising.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;22. The Confounding 'String' Concatenation&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;String concatenation in JavaScript can sometimes lead to unexpected results:&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;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;2&lt;/span&gt;&lt;span class="dl"&gt;"&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="c1"&gt;// Output: "22"&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: "22"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript's loose typing can lead to implicit type coercion, resulting in strings being concatenated instead of numbers being added.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;23. The Eccentric 'Array Length' Property&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The length property of arrays can be manipulated directly, leading to unexpected outcomes:&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;arr&lt;/span&gt; &lt;span class="o"&gt;=&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="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="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="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;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: []&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Setting the length property of an array to a smaller value truncates the array, effectively removing elements from the end.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;24. The Quirky 'Date' Object&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Working with dates in JavaScript can be quite peculiar, especially with months indexed from 0:&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;date&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;Date&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2024&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="mi"&gt;10&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;date&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 2024-04-10T00:00:00.000Z&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;3&lt;/code&gt; represents &lt;code&gt;April&lt;/code&gt;, &lt;code&gt;not March&lt;/code&gt;, which can catch developers off guard if they're not accustomed to it.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;25. The Curious Case of 'undefined'&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Accessing properties of undefined variables doesn't throw an error in JavaScript; instead, it returns undefined:&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;obj&lt;/span&gt; &lt;span class="o"&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;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While this behavior can be convenient, it can also hide potential bugs, especially when dealing with deeply nested structures.&lt;/p&gt;

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

&lt;p&gt;In conclusion, our journey through the eccentricities of JavaScript has revealed a language filled with surprises and peculiarities. From its quirky type coercion to its unexpected behaviors with arrays and dates, JavaScript keeps developers on their toes. Despite its idiosyncrasies, JavaScript remains a powerful tool for creating dynamic web experiences. By understanding and embracing its eccentricities, developers can navigate the intricacies of JavaScript with confidence, unlocking its full potential while enjoying the occasional quirk along the way.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Functional Programming in JavaScript: 10 Scenarios Demystified</title>
      <dc:creator>MD. JAHID HOSSAIN</dc:creator>
      <pubDate>Wed, 10 Apr 2024 13:31:44 +0000</pubDate>
      <link>https://forem.com/jahid6597/mastering-real-world-functional-programming-in-javascript-10-scenarios-demystified-48n4</link>
      <guid>https://forem.com/jahid6597/mastering-real-world-functional-programming-in-javascript-10-scenarios-demystified-48n4</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Introduction:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Functional programming (FP) has become increasingly popular in JavaScript development for its ability to write cleaner, more maintainable and scalable code. However, understanding FP concepts and applying them in real-world scenarios can be daunting. In this comprehensive guide, we'll explore 10 real-life scenarios where functional programming shines in JavaScript. Each scenario will be accompanied by theoretical explanations, practical code examples and detailed explanations, providing you with the tools to master FP in your JavaScript projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;1. Online Shopping Cart: Filtering Out Expired Items&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Theory:&lt;/strong&gt; Functional programming encourages using higher-order functions like &lt;code&gt;filter&lt;/code&gt; for data manipulation tasks. In this scenario, we'll filter out expired items from an online shopping cart.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; We'll define a predicate function to determine if an item is expired and use the &lt;code&gt;filter&lt;/code&gt; function to remove expired items from the cart.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&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;cart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...];&lt;/span&gt; &lt;span class="c1"&gt;// Array of items in the shopping cart&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isItemExpired&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;expiryDate&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&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;updatedCart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;isItemExpired&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We define a predicate function &lt;code&gt;isItemExpired&lt;/code&gt; that checks if an item's expiry date is in the past.&lt;/li&gt;
&lt;li&gt;We use the &lt;code&gt;filter&lt;/code&gt; function to create a new array &lt;code&gt;updatedCart&lt;/code&gt; containing only the items that are not expired.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;2. Social Media Feed: Mapping Usernames to User Profiles&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Theory:&lt;/strong&gt; Functional programming promotes using &lt;code&gt;map&lt;/code&gt; to transform data from one form to another. In this scenario, we'll map usernames to user profiles in a social media feed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; We'll extract usernames from a list of posts and map them to corresponding user profiles fetched from a database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&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;posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...];&lt;/span&gt; &lt;span class="c1"&gt;// Array of social media posts&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userProfileDB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{...};&lt;/span&gt; &lt;span class="c1"&gt;// Object containing user profiles indexed by username&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getUserProfile&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;userProfileDB&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;postsWithUserProfiles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;posts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&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;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;userProfile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;getUserProfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&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;&lt;strong&gt;Code Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We define a function &lt;code&gt;getUserProfile&lt;/code&gt; to retrieve a user's profile from the database based on their username.&lt;/li&gt;
&lt;li&gt;We use the &lt;code&gt;map&lt;/code&gt; function to create a new array &lt;code&gt;postsWithUserProfiles&lt;/code&gt; where each post is augmented with its author's user profile.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;3. Task Management App: Reducing Pending Tasks&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Theory:&lt;/strong&gt; Functional programming promotes using &lt;code&gt;reduce&lt;/code&gt; to perform operations on lists and accumulate results. In this scenario, we'll calculate the total number of pending tasks in a task management app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; We'll use the &lt;code&gt;reduce&lt;/code&gt; function to iterate over a list of tasks and count the number of pending tasks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&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;tasks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...];&lt;/span&gt; &lt;span class="c1"&gt;// Array of task objects&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;countPendingTasks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;task&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pending&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;acc&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="nx"&gt;acc&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;totalPendingTasks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;countPendingTasks&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We define a &lt;code&gt;reducer&lt;/code&gt; function &lt;code&gt;countPendingTasks&lt;/code&gt; that increments the accumulator if a task's status is 'pending'.&lt;/li&gt;
&lt;li&gt;We use the &lt;code&gt;reduce&lt;/code&gt; function to iterate over the tasks array, applying the reducer function to calculate the total number of pending tasks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;4. Email Client: Composing Email Templates&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Theory:&lt;/strong&gt; Functional programming encourages composing functions to create more complex behaviors. In this scenario, we'll compose functions to generate email templates for different types of emails.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; We'll create reusable functions for generating different parts of an email template (e.g., subject, body) and compose them to generate complete email templates.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&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;createEmailSubject&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;`Regarding &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="s2"&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;createEmailBody&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;`Dear user,\n\n&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;\n\nRegards,\nYour Email Client`&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;composeEmailTemplate&lt;/span&gt; &lt;span class="o"&gt;=&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;content&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="na"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;createEmailSubject&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="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;createEmailBody&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;content&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;welcomeEmail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;composeEmailTemplate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Welcome&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="s1"&gt;Welcome to our platform!&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;&lt;strong&gt;Code Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We define functions &lt;code&gt;createEmailSubject&lt;/code&gt; and &lt;code&gt;createEmailBody&lt;/code&gt; to generate the subject and body of an email, respectively.&lt;/li&gt;
&lt;li&gt;We use the &lt;code&gt;composeEmailTemplate&lt;/code&gt; function to compose the subject and body into a complete email template for a given type and content.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;5. Fitness App: Calculating Calorie Burn&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Theory:&lt;/strong&gt; Functional programming promotes using recursion for repetitive tasks. In this scenario, we'll use recursion to calculate the total calorie burn for a workout routine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; We'll define a recursive function to calculate the total calorie burn by summing the calorie burn for each exercise in the workout routine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&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;calculateCalorieBurn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;exercises&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;exercises&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;currentExercise&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;remainingExercises&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;exercises&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;currentExercise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;caloriesBurned&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;calculateCalorieBurn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;remainingExercises&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;workoutRoutine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...];&lt;/span&gt; &lt;span class="c1"&gt;// Array of exercise objects&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;totalCalorieBurn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculateCalorieBurn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;workoutRoutine&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We define a recursive function &lt;code&gt;calculateCalorieBurn&lt;/code&gt; that takes an array of exercises and calculates the total calorie burn by summing the calorie burn for each exercise.&lt;/li&gt;
&lt;li&gt;The base case of the recursion is when there are no more exercises left, in which case the total calorie burn is 0.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;6. Blogging Platform: Formatting Markdown Content&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Theory:&lt;/strong&gt; Functional programming promotes using pure functions for data transformation. In this scenario, we'll use pure functions to format Markdown content for display on a blogging platform.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; We'll define pure functions to convert Markdown content to HTML and add additional styling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&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;convertMarkdownToHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;markdownContent&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="c1"&gt;// Logic to convert Markdown to HTML&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;addStylingToHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;htmlContent&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="c1"&gt;// Logic to add styling to HTML&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;markdownContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;## Title&lt;/span&gt;&lt;span class="se"&gt;\n\n&lt;/span&gt;&lt;span class="s1"&gt;This is a **bold** text.&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;formattedHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;addStylingToHTML&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;convertMarkdownToHTML&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;markdownContent&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We define two pure functions &lt;code&gt;convertMarkdownToHTML&lt;/code&gt; and &lt;code&gt;addStylingToHTML&lt;/code&gt; to convert Markdown content to HTML and add styling, respectively.&lt;/li&gt;
&lt;li&gt;We chain the functions together to format the Markdown content and apply styling, resulting in formatted HTML for display.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;7. Financial App: Calculating Compound Interest&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Theory:&lt;/strong&gt; Functional programming promotes using immutable data structures to avoid unintended side effects. In this scenario, we'll calculate compound interest using immutable data structures.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; We'll define a function to calculate compound interest without mutating the input variables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&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;calculateCompoundInterest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;principal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;time&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;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;principal&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;rate&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;time&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;interest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;principal&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;amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;interest&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;principal&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rate&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="c1"&gt;// Annual interest rate in percentage&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;time&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="c1"&gt;// Number of years&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;compoundInterest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;calculateCompoundInterest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;principal&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We define a function &lt;code&gt;calculateCompoundInterest&lt;/code&gt; that takes the principal amount, annual interest rate, and time period as input and returns the compound interest amount.&lt;/li&gt;
&lt;li&gt;Inside the function, we calculate the compound interest amount using immutable calculations without mutating the input variables.&lt;/li&gt;
&lt;li&gt;The result is returned as an object containing the total amount and the interest accrued.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;8. E-commerce Platform: Sorting Products by Price&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Theory:&lt;/strong&gt; Functional programming promotes using higher-order functions for list processing tasks. In this scenario, we'll use the &lt;code&gt;sort&lt;/code&gt; function to sort products by price.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; We'll define a comparator function to specify the sorting order and use the &lt;code&gt;sort&lt;/code&gt; function to sort products by price.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&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;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...];&lt;/span&gt; &lt;span class="c1"&gt;// Array of product objects&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;sortByPrice&lt;/span&gt; &lt;span class="o"&gt;=&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="o"&gt;=&amp;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;price&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="nx"&gt;price&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;sortedProducts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sortByPrice&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We define a comparator function &lt;code&gt;sortByPrice&lt;/code&gt; that compares two products based on their price.&lt;/li&gt;
&lt;li&gt;We use the &lt;code&gt;sort&lt;/code&gt; function to sort the array of products using the &lt;code&gt;sortByPrice&lt;/code&gt; comparator function, resulting in a new array of products sorted by price.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;9. Music Streaming App: Filtering Songs by Genre&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Theory:&lt;/strong&gt; Functional programming promotes using declarative code for readability. In this scenario, we'll use the &lt;code&gt;filter&lt;/code&gt; function to filter songs by genre in a music streaming app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; We'll define a predicate function to filter songs by genre and use the &lt;code&gt;filter&lt;/code&gt; function to apply the filter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&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;songs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...];&lt;/span&gt; &lt;span class="c1"&gt;// Array of song objects&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;filterSongsByGenre&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;genre&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;song&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;song&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;genre&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;genre&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;genre&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rock&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;rockSongs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;songs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;filterSongsByGenre&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;genre&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We define a higher-order function &lt;code&gt;filterSongsByGenre&lt;/code&gt; that takes a genre as input and returns a predicate function to filter songs by that genre.&lt;/li&gt;
&lt;li&gt;We use the &lt;code&gt;filter&lt;/code&gt; function to create a new array &lt;code&gt;rockSongs&lt;/code&gt; containing only the songs that match the specified genre.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;10. Weather App: Predicting Weather Trends&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Theory:&lt;/strong&gt; Functional programming promotes using recursion for repetitive tasks. In this scenario, we'll use recursion to predict weather trends based on historical data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt; We'll define a recursive function to analyze historical weather data and predict weather trends for the future.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code:&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;predictWeatherTrend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;historicalData&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="c1"&gt;// Logic to analyze historical data and predict weather trend&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;historicalWeatherData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...];&lt;/span&gt; &lt;span class="c1"&gt;// Array of historical weather data objects&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;weatherTrend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;predictWeatherTrend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;historicalWeatherData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We define a recursive function &lt;code&gt;predictWeatherTrend&lt;/code&gt; that takes historical weather data as input and analyzes it to predict weather trends.&lt;/li&gt;
&lt;li&gt;The function recursively analyzes historical data to identify patterns and predict future weather trends.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;In conclusion, functional programming provides a powerful and intuitive approach to solving real-world problems in JavaScript. By emphasizing the use of higher-order functions like &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;sort&lt;/code&gt; and &lt;code&gt;reduce&lt;/code&gt;, developers can efficiently manipulate data, streamline workflows, and create more maintainable code.&lt;/p&gt;

&lt;p&gt;Through scenarios ranging from filtering online shopping carts to sorting restaurant reservations, we've seen how functional programming principles enhance code readability, promote modularity, and simplify complex tasks. Whether you're analyzing user interactions, prioritizing tasks, or formatting content, functional programming offers elegant solutions that make your code more concise and expressive.&lt;/p&gt;

&lt;p&gt;By embracing functional programming in your JavaScript projects, you'll not only improve your coding skills but also unlock new possibilities for creating cleaner, more scalable, and more reliable applications. So, dive into functional programming, explore its features, and unleash the full potential of JavaScript in your development journey.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Shallow Copy VS Deep Copy</title>
      <dc:creator>MD. JAHID HOSSAIN</dc:creator>
      <pubDate>Thu, 28 Dec 2023 22:31:23 +0000</pubDate>
      <link>https://forem.com/jahid6597/beyond-cloning-sculpting-javascript-mirrors-with-the-art-of-shallow-copy-and-the-depth-of-deep-copy-3p64</link>
      <guid>https://forem.com/jahid6597/beyond-cloning-sculpting-javascript-mirrors-with-the-art-of-shallow-copy-and-the-depth-of-deep-copy-3p64</guid>
      <description>&lt;p&gt;In JavaScript, the concepts of shallow copy and deep copy are related to the duplication of objects, arrays, or other complex data structures. Understanding the difference between these two types of copies is crucial for managing data effectively and preventing unintended side effects in your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Shallow Copy:&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A shallow copy creates a new object or array and copies the elements of the original one to the new one. However, it does not create copies of nested objects or arrays; instead, it copies references to them.&lt;/li&gt;
&lt;li&gt;Shallow copying is performed using methods like &lt;code&gt;Object.assign()&lt;/code&gt;, the &lt;code&gt;spread operator (...)&lt;/code&gt;, or array methods like &lt;code&gt;slice()&lt;/code&gt; or &lt;code&gt;concat()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Shallow copies are faster and consume less memory since they only copy references. However, they may lead to unexpected behavior if the original and copied objects share references to nested objects.
&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;// original object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;originalObject&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;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wonderland&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JavaScript&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="c1"&gt;// Shallow copy&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;shallowCopy&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="nx"&gt;originalObject&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Let's change a nested property&lt;/span&gt;
&lt;span class="nx"&gt;shallowCopy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mirrorland&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="nx"&gt;originalObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Mirrorland&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Deep Copy:&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A deep copy, on the other hand, creates a completely independent copy of the original object or array, including all nested objects and arrays.&lt;/li&gt;
&lt;li&gt;Achieving a deep copy can be more complex and usually requires custom implementation, such as using recursion to traverse the entire structure and create new instances for each nested element.&lt;/li&gt;
&lt;li&gt;Deep copies provide true isolation between the original and copied data, ensuring that changes to one do not affect the other. However, they can be slower and consume more memory, especially for large and complex data structures.
&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;// Deep copy using a utility function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;deepCopy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&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;typeof&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;obj&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;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;obj&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;newObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&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;span class="k"&gt;for &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;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;obj&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;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hasOwnProperty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;newObj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;deepCopy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&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;return&lt;/span&gt; &lt;span class="nx"&gt;newObj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// original object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;originalObject&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;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;city&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wonderland&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;JavaScript&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="c1"&gt;// Deep copy&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;copyObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;deepCopy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalObject&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Let's change a nested property&lt;/span&gt;
&lt;span class="nx"&gt;copyObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mirrorland&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="nx"&gt;originalObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;city&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: Wonderland&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Pros and Cons&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Shallow Copy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt; Lightweight, maintains references, suitable for simple structures.&lt;br&gt;
&lt;strong&gt;Cons:&lt;/strong&gt; Changes to nested structures affect both original and copy, may lead to unexpected behavior."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deep Copy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros:&lt;/strong&gt; Independent duplicates, changes do not affect the original, suitable for complex structures.&lt;br&gt;
&lt;strong&gt;Cons:&lt;/strong&gt; May be computationally expensive, potential issues with circular references."&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Best Practices&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use shallow copy when dealing with simple structures and you want to maintain lightweight references.&lt;/li&gt;
&lt;li&gt;Opt for deep copy when working with complex structures or when you need completely independent duplicates.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;In the enchanting world of JavaScript, cloning objects goes beyond the basics of duplication. Shallow copy and deep copy are two sides of the same mirror, each with its own strengths and weaknesses. The choice between them depends on the specific needs of your application.&lt;/p&gt;

&lt;p&gt;So, as you venture into the mystical realms of JavaScript development, remember that a mirror is not just a reflection; it's a portal to the depths of your data. Embrace the art of cloning, and may your mirrors be as magical as Alice's in the Wonderland of JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Happy coding!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript Execution Context: A Deep Dive</title>
      <dc:creator>MD. JAHID HOSSAIN</dc:creator>
      <pubDate>Thu, 09 Mar 2023 17:46:57 +0000</pubDate>
      <link>https://forem.com/jahid6597/javascript-execution-context-a-deep-dive-4kno</link>
      <guid>https://forem.com/jahid6597/javascript-execution-context-a-deep-dive-4kno</guid>
      <description>&lt;h2&gt;
  
  
  Real Life Example Of Execution Context
&lt;/h2&gt;

&lt;p&gt;Suppose you're the manager of a restaurant, and your restaurant is like a JavaScript program. Each section of the restaurant represents an execution context.&lt;/p&gt;

&lt;p&gt;The first section of the restaurant is the dining room, which is like the global execution context in JavaScript. This is the main area where the customers come to eat and enjoy their meals. It contains everything that's required for the restaurant to run smoothly, like the tables, chairs, menus, and waitstaff. This is similar to how the global execution context contains all the variables, functions, and objects that are needed for a JavaScript program to run.&lt;/p&gt;

&lt;p&gt;Now, let's say a customer wants to order a meal. The customer is like the user input, and the act of taking their order is like creating a new execution context. The waiter takes the customer's order and moves to the kitchen area, which is like creating a new execution context in JavaScript.&lt;/p&gt;

&lt;p&gt;In the kitchen area, the chef and kitchen staff prepare the meal based on the order taken by the waiter. This area is like a new execution context, similar to how each function call in JavaScript creates a new execution context. The kitchen has its own set of tools and ingredients, just like each execution context in JavaScript has its own set of variables and functions.&lt;/p&gt;

&lt;p&gt;Once the meal is prepared, it's delivered to the customer in the dining room. This is like the result of a function call being returned to the global execution context. The customer receives their meal, and the restaurant continues to operate smoothly.&lt;/p&gt;

&lt;p&gt;Overall, just like how a restaurant operates with different areas serving different functions, JavaScript code operates in different execution contexts as it moves through a program.&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript
&lt;/h2&gt;

&lt;p&gt;JavaScript is a high-level programming language used for web development. It is single-threaded, meaning it can only execute one command at a time, and is interpreted, meaning it is not compiled before execution.&lt;/p&gt;

&lt;p&gt;V8 is a high-performance JavaScript engine developed by Google and used in their Chrome browser, as well as other applications. It compiles JavaScript code to machine code, improving its performance.&lt;/p&gt;

&lt;p&gt;JavaScript is primarily used for client-side scripting, meaning it runs on the user's web browser, and is used to create dynamic and interactive web pages. It is often used in conjunction with HTML and CSS.&lt;/p&gt;

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

&lt;p&gt;JavaScript Execution Context is the environment in which JavaScript code is executed. It contains information about the variables, functions, and objects that are available to the code being executed, as well as the scope chain and the value of the 'this' keyword.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;creation phase&lt;/li&gt;
&lt;li&gt;execution phase&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Creation Phase:&lt;/strong&gt;&lt;br&gt;
In this phase, the JavaScript engine sets up the environment for the code to be executed. During this phase, the JavaScript engine creates the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The Variable Object (VO):&lt;/strong&gt; The VO contains all the variables and functions that are defined in the current scope. This includes function arguments, function declarations, and variable declarations. The VO is used to resolve identifiers to their values during execution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Scope Chain:&lt;/strong&gt; The Scope Chain is a list of Variable Objects that are accessible in the current scope. Each Variable Object in the Scope Chain represents a higher level of scope.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The "this" keyword:&lt;/strong&gt; The "this" keyword is set to the value of the "this" object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Execution Phase:&lt;/strong&gt;&lt;br&gt;
In this phase, the JavaScript engine executes the code line by line. The JavaScript engine reads the code and executes it one line at a time. This phase involves the following steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Assigning Values to Variables:&lt;/strong&gt; During the execution phase, the JavaScript engine assigns values to variables. If a variable is not initialized, it has the value of 'undefined'.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Executing Functions and Code Blocks:&lt;/strong&gt; The JavaScript engine executes functions and code blocks as it encounters them in the code. If a function is called, the engine creates a new execution context for that function and adds it to the call stack.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Managing the Call Stack:&lt;/strong&gt; The call stack is a data structure that keeps track of the functions that are being executed. When a function is called, its execution context is added to the top of the call stack. When the function returns, its execution context is removed from the stack.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;The two main components of an execution context in JavaScript are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memory Component:&lt;/strong&gt; This refers to the memory space that is allocated for the code and data components within the context. This includes variables, objects, arrays, and other data structures that are used or manipulated by the code. The memory component is also responsible for maintaining the scope chain, which is a list of variable objects that a function has access to, starting with its own variable object and continuing with the variable objects of its parent functions, all the way up to the global variable object.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Component:&lt;/strong&gt; This refers to the actual code that is being executed within the context. It includes any function and variable declarations, as well as any other instructions that make up the code. During the creation phase of the execution context, the JavaScript engine sets up memory space for all variables and function declarations through a process known as hoisting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpqc3ltiimyy4n74tw9wm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpqc3ltiimyy4n74tw9wm.png" alt=" " width="517" height="156"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Together, these two components enable the JavaScript engine to execute code and manage data within the program. Understanding how these components work is crucial for writing efficient and effective JavaScript code.&lt;/p&gt;

&lt;p&gt;Let's take an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greetings&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Welcome to the JS world!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;number1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;number2&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="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;number1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;number2&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;number1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;number2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addExtra&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;number2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;extra&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;15&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;number1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;number2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;extra&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;result1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;number2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;result2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;addExtra&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;number2&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;result1&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;result2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initially call stack is empty &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcgmi1qfu7u4msqxv67ly.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcgmi1qfu7u4msqxv67ly.png" alt=" " width="281" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The JavaScript engine first runs the complete source code then does the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a Global Execution Context(G.E.C) and push it to the call stack.&lt;/li&gt;
&lt;li&gt;Creates a global object window in the browser and global in the NodeJs.&lt;/li&gt;
&lt;li&gt;Stores the variables with values as undefined and function body references.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Push Global Execution Context into the call stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7orfpc44zov4pgo2nl5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7orfpc44zov4pgo2nl5.png" alt=" " width="281" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Memory Creation for the Global Execution Context&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy34f5qfyagrxbr7ahh6z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy34f5qfyagrxbr7ahh6z.png" alt=" " width="672" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After this memory creation phase, the execution context will move to the code execution phase.&lt;br&gt;
The &lt;code&gt;greetings&lt;/code&gt; function is called.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw3v54m2ynv6mg3g71wbp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw3v54m2ynv6mg3g71wbp.png" alt=" " width="676" height="297"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create greetings Execution Context and push it to the call stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F71z1mjpbzphztar4ergb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F71z1mjpbzphztar4ergb.png" alt=" " width="281" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Memory Creation and Code Execution of greetings Execution Context. Here The &lt;code&gt;greetings&lt;/code&gt; function is called, so it executes and logs the string "Welcome to the JS world!" to the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fesbtxq1qh75ob55bz7o8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fesbtxq1qh75ob55bz7o8.png" alt=" " width="761" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Console&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febhli3oxr8kblk3gm86b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Febhli3oxr8kblk3gm86b.png" alt=" " width="281" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pop greetings Execution Context from the call stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7orfpc44zov4pgo2nl5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7orfpc44zov4pgo2nl5.png" alt=" " width="281" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;variable &lt;code&gt;number1&lt;/code&gt; defined and assigned value 10&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm6otp14482r2o5tv8awa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm6otp14482r2o5tv8awa.png" alt=" " width="721" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;variable &lt;code&gt;number2&lt;/code&gt; defined and assigned value 5&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftwmeyhwkhuq2tmofr20h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftwmeyhwkhuq2tmofr20h.png" alt=" " width="721" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A function &lt;code&gt;add&lt;/code&gt; is called, which takes two parameters &lt;code&gt;number1 = 10&lt;/code&gt; and &lt;code&gt;number2 = 5&lt;/code&gt;. Inside the function, the two parameters are added together and the &lt;code&gt;result = 15&lt;/code&gt; is returned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fochbvddgtpn5eymw6bgy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fochbvddgtpn5eymw6bgy.png" alt=" " width="721" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create add Execution Context and push it to the call stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzfqr13gg44bcsml6zavi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzfqr13gg44bcsml6zavi.png" alt=" " width="281" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Memory Creation and Code Execution of add Execution Context. Here returned the sum of &lt;code&gt;number1&lt;/code&gt; and &lt;code&gt;number2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsdzelimnvg06piip1ecz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsdzelimnvg06piip1ecz.png" alt=" " width="761" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;variable &lt;code&gt;result1&lt;/code&gt; defined and assigned the result of calling the &lt;code&gt;add&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1d1ijba83d3l9674zsk3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1d1ijba83d3l9674zsk3.png" alt=" " width="721" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pop add Execution Context from the call stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7orfpc44zov4pgo2nl5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7orfpc44zov4pgo2nl5.png" alt=" " width="281" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A function &lt;code&gt;addExtra&lt;/code&gt; is called, which takes two parameters &lt;code&gt;number1&lt;/code&gt; and &lt;code&gt;number2&lt;/code&gt;. Inside the function, a new variable &lt;code&gt;extra&lt;/code&gt; is defined and assigned a value of 15. Then, the sum of &lt;code&gt;number1&lt;/code&gt;, &lt;code&gt;number2&lt;/code&gt;, and &lt;code&gt;extra&lt;/code&gt; is returned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2xuf8fgd9nsdc7nzs2hp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2xuf8fgd9nsdc7nzs2hp.png" alt=" " width="721" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create addExtra Execution Context and push it to the call stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyja3gqpcrub20ya3dttp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyja3gqpcrub20ya3dttp.png" alt=" " width="281" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Memory Creation and Code Execution of addExtra Execution Context. Here &lt;code&gt;extra&lt;/code&gt; is defined and assigned a value of 15 and returned the sum of &lt;code&gt;number1&lt;/code&gt;, &lt;code&gt;number2&lt;/code&gt; and &lt;code&gt;extra&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;variable &lt;code&gt;result2&lt;/code&gt; defined and assigned the result of calling the &lt;code&gt;addExtra&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlqat5zk8zcu5z45ppxa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlqat5zk8zcu5z45ppxa.png" alt=" " width="721" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pop addExtra Execution Context from the call stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7orfpc44zov4pgo2nl5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7orfpc44zov4pgo2nl5.png" alt=" " width="281" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The value of &lt;code&gt;result1 = 15&lt;/code&gt; logged to the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7k563z5m2obd2321hc3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx7k563z5m2obd2321hc3.png" alt=" " width="721" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Console&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ahwkc7auryxaao7bhot.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ahwkc7auryxaao7bhot.png" alt=" " width="281" height="151"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The value of &lt;code&gt;result2 = 30&lt;/code&gt; logged to the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh0irupf3oamygq5paa7f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh0irupf3oamygq5paa7f.png" alt=" " width="721" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Console&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffwn6n17uujysmnz58ig6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffwn6n17uujysmnz58ig6.png" alt=" " width="281" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pop Global Execution Context from the call stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcgmi1qfu7u4msqxv67ly.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcgmi1qfu7u4msqxv67ly.png" alt=" " width="281" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;JavaScript execution context is a fundamental concept that defines the environment in which JavaScript code is executed. It plays a vital role in how JavaScript functions and interacts with its surrounding environment, and understanding it is essential to write efficient and effective JavaScript code. An execution context is a data structure that contains information about the environment in which the JavaScript code is executed.&lt;/p&gt;

&lt;p&gt;Each execution context has a scope chain that determines what variables and functions the context has access to. The scope chain is determined by the lexical environment of the execution context and is used to look up variables and functions when code is executed.&lt;/p&gt;

&lt;p&gt;The variable object is another important component of an execution context. It is a data structure that contains information about the variables, functions, and parameters defined in the execution context. The variable object is used to store and manage the variables and functions in the context and is updated dynamically as the code is executed.&lt;/p&gt;

&lt;p&gt;The this keyword is also an important part of the execution context. The value of this is determined by the context in which a function is called. In the global execution context, this refers to the global object. In a function execution context, this refers to the object that the function is a method of, or the global object if the function is not a method of any object.&lt;/p&gt;

&lt;p&gt;Hoisting is another concept that is closely related to execution context. In JavaScript, variable and function declarations are hoisted to the top of their respective execution contexts. This means that they can be used before they are declared in the code.&lt;/p&gt;

&lt;p&gt;Finally, the execution stack is a data structure that stores execution contexts in a last-in, first-out (LIFO) order. When a function is called, a new execution context is created and pushed onto the top of the stack. When a function returns, its execution context is popped off the stack.&lt;/p&gt;

&lt;p&gt;The execution context is a fundamental concept in JavaScript that defines the environment in which code is executed. It contains information about the variables, functions, and parameters defined in that environment, as well as the scope chain and the this keyword. By understanding the execution context, developers can write more efficient and effective JavaScript code, ultimately leading to better performance and more reliable applications.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>discuss</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Difference between Promises and Observables</title>
      <dc:creator>MD. JAHID HOSSAIN</dc:creator>
      <pubDate>Sat, 11 Feb 2023 19:09:03 +0000</pubDate>
      <link>https://forem.com/jahid6597/difference-between-promises-and-observables-2a2p</link>
      <guid>https://forem.com/jahid6597/difference-between-promises-and-observables-2a2p</guid>
      <description>&lt;p&gt;Imagine that you have a friend who promises to give you a toy. A promise is like that - it's when someone says they'll do something for you in the future. So when your friend promises to give you a toy, you trust that they will do it.&lt;/p&gt;

&lt;p&gt;Now, imagine that you're waiting for your friend to bring you the toy, but you don't know when they'll arrive. This is kind of like an observable. An observable is like waiting for something to happen, but you don't know exactly when it will happen. It's like watching a caterpillar turn into a butterfly - you know it will happen, but you don't know exactly when.&lt;/p&gt;

&lt;p&gt;In computer programming, promises and observables are used to help us do things in a certain order. For example, we might use a promise to wait for data to be loaded from a website, and then use an observable to show that data on the screen. Just like waiting for your friend to bring you a toy, promises and observables help us trust that certain things will happen in the right order.&lt;/p&gt;

&lt;p&gt;Promises and observables are both design patterns used for handling asynchronous operations. However, they are different in their implementation and use cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Promise
&lt;/h2&gt;

&lt;p&gt;A Promise is a single-value container that represents the eventual result of an asynchronous operation. Once a promise is resolved or rejected, its value can not change anymore. It is often used when you want to handle the result of an async operation, like an HTTP call. Promises are commonly used to handle async operations that return a single result.&lt;/p&gt;

&lt;p&gt;A promise represents a value that may not be available yet. It's a returned object to which you attach callbacks, instead of passing callbacks into a function. A Promise has two states: "pending" and "settled". A promise is said to be "pending" when it has not yet been fulfilled or rejected. A promise is said to be "settled" when it has either been fulfilled with a value or rejected with a reason (error).&lt;/p&gt;

&lt;p&gt;Here's an example of using a Promise to handle an asynchronous operation:&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;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;xhr&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;XMLHttpRequest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;GET&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onload&lt;/span&gt; &lt;span class="o"&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="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;responseText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onerror&lt;/span&gt; &lt;span class="o"&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="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;statusText&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;xhr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&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;getData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://jsonplaceholder.typicode.com/posts&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;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;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="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="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;error&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;p&gt;In this example, we use the &lt;code&gt;getData&lt;/code&gt; function to make an HTTP &lt;code&gt;GET&lt;/code&gt; request to a URL and retrieve the response text. The &lt;code&gt;getData&lt;/code&gt; function returns a &lt;code&gt;Promise&lt;/code&gt; that is resolved with the response text when the request is successful, or rejected with an error message when the request fails. The Promise is used by calling the then method and passing in a success callback, as well as a &lt;code&gt;catch&lt;/code&gt; method to handle any errors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Observable
&lt;/h2&gt;

&lt;p&gt;An Observable is a sequence of values that can be observed over time. It's like a stream that can emit multiple values over time, and it can be used for handling asynchronous data such as events or data from an API. An Observable can be "subscribed" to and whenever a new value is emitted, the subscriber will receive it.&lt;/p&gt;

&lt;p&gt;An Observable is a stream of values over time. An observable can emit multiple values over a period of time, and you can subscribe to it to receive each value as it comes in. It is similar to a promise but it is a representation of a series of future values or events. Observables are more versatile and can be used for handling more complex async operations, like real-time updates, user input, and handling multiple values over time.&lt;/p&gt;

&lt;p&gt;Here's an example of using an Observable to handle an asynchronous operation:&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;Observable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fromEvent&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;rxjs&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;button&lt;/span&gt; &lt;span class="o"&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;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button&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;clickObservable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fromEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;button&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="nx"&gt;clickObservable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&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;=&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;event&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we use the &lt;code&gt;fromEvent&lt;/code&gt; function from the &lt;code&gt;rxjs&lt;/code&gt; library to create an Observable that represents a stream of button click events. We then &lt;code&gt;subscribe&lt;/code&gt; to the &lt;code&gt;Observable&lt;/code&gt; using the &lt;code&gt;subscribe&lt;/code&gt; method, and whenever the button is clicked, the &lt;code&gt;subscriber&lt;/code&gt; will receive the &lt;code&gt;event&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Choosing between a Promise and an Observable will depend on the specific requirements of your application. If you need to handle a single asynchronous result, then a Promise is probably the better option. However, if you need to handle multiple values over time, or if you want to be able to cancel an ongoing operation, then an Observable would be a better choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Difference Table
&lt;/h2&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;Promise&lt;/th&gt;
&lt;th&gt;Observable&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Representation&lt;/td&gt;
&lt;td&gt;Single-value container&lt;/td&gt;
&lt;td&gt;Stream of values over time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Number of Values&lt;/td&gt;
&lt;td&gt;Single-value container&lt;/td&gt;
&lt;td&gt;Multiple values over time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Use Case&lt;/td&gt;
&lt;td&gt;Ideal for handling a single result of an async operation&lt;/td&gt;
&lt;td&gt;Ideal for handling multiple values over time, like real-time updates, user input, etc.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Immutability&lt;/td&gt;
&lt;td&gt;Value cannot be changed once it is resolved or rejected&lt;/td&gt;
&lt;td&gt;Observables can change values over time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Syntax&lt;/td&gt;
&lt;td&gt;Simpler syntax&lt;/td&gt;
&lt;td&gt;More complex syntax&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Error Handling&lt;/td&gt;
&lt;td&gt;Reject callback for error handling&lt;/td&gt;
&lt;td&gt;Error handling through catch and/or onError callbacks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Chaining&lt;/td&gt;
&lt;td&gt;Can chain multiple then callbacks&lt;/td&gt;
&lt;td&gt;Can chain multiple subscribers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lazy Evaluation&lt;/td&gt;
&lt;td&gt;Evaluates immediately&lt;/td&gt;
&lt;td&gt;Can be lazily evaluated by subscribing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Composability&lt;/td&gt;
&lt;td&gt;Difficult to compose Promises&lt;/td&gt;
&lt;td&gt;Easy to compose and manipulate Observables using operators&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cancellation&lt;/td&gt;
&lt;td&gt;No built-in support for cancellation&lt;/td&gt;
&lt;td&gt;Supports cancellation through unsubscribing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Blocking&lt;/td&gt;
&lt;td&gt;Blocking - waits for the result&lt;/td&gt;
&lt;td&gt;Non-blocking - emits values as they come in&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Creation&lt;/td&gt;
&lt;td&gt;&lt;code&gt; const promise = new Promise(() =&amp;gt; { &lt;br&gt; resolve(10); &lt;br&gt; }); &lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt; const obs = new Observable((observer) =&amp;gt; { &lt;br&gt; observer.next(10); &lt;br&gt; }); &lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Transform&lt;/td&gt;
&lt;td&gt;&lt;code&gt; promise.then((value) =&amp;gt; value * 2); &lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt; Obs.pipe(map(value) =&amp;gt; value * 2); &lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Subscribe&lt;/td&gt;
&lt;td&gt;&lt;code&gt; promise.then((value) =&amp;gt; { &lt;br&gt; console.log(value); &lt;br&gt; }); &lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt; const sub = obs.subscribe((value) =&amp;gt; { &lt;br&gt; console.log(value); &lt;br&gt; }); &lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Unsubscribe&lt;/td&gt;
&lt;td&gt;Can’t unsubscribe&lt;/td&gt;
&lt;td&gt;&lt;code&gt; sub.unsubscribe(); &lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  When we should use
&lt;/h2&gt;

&lt;p&gt;Promises and Observables are used in different situations, depending on the nature of the asynchronous operation you're trying to perform.&lt;/p&gt;

&lt;p&gt;Promises are typically used when you have a single asynchronous operation that needs to be performed, and you want to be notified when that operation has completed (or if it fails). For example, you might use a Promise when you need to make an HTTP request to retrieve data from a server, and you want to handle the response when it arrives.&lt;/p&gt;

&lt;p&gt;Observables, on the other hand, are typically used when you have a sequence of asynchronous events that you want to observe over time. For example, you might use an Observable when you want to subscribe to a stream of mouse clicks or keyboard events, and you want to handle each event as it occurs.&lt;/p&gt;

&lt;p&gt;In general, if you have a single asynchronous operation that you want to perform, and you just want to be notified when it has completed, a Promise is a good choice. If you have a sequence of events that you want to observe over time, an Observable is a good choice.&lt;/p&gt;

&lt;p&gt;Ultimately, the choice between a Promise and an Observable will depend on the specific requirements of your application, and it's important to understand the differences between the two concepts in order to make the right choice for your use case.&lt;/p&gt;

&lt;h2&gt;
  
  
  When we should not use
&lt;/h2&gt;

&lt;p&gt;We should not use Promises or Observables in situations where they are not appropriate. Here are a few examples of when you might want to avoid using either one:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid using Promises when you need to observe a stream of events over time, rather than a single value that may or may not be available. In this case, an Observable is a better choice.&lt;/li&gt;
&lt;li&gt;Avoid using Observables when you have a single asynchronous operation that you just need to perform, and you don't care about observing the results over time. In this case, a Promise is a better choice.&lt;/li&gt;
&lt;li&gt;Avoid using Promises or Observables when you can perform the operation synchronously, without involving any asynchronous behavior. In this case, using a synchronous solution is a better choice.&lt;/li&gt;
&lt;li&gt;Avoid using either Promises or Observables in situations where you need tight control over the order in which events occur. For example, if you need to perform a set of operations in a specific order, and you need to guarantee that one operation is complete before starting the next, you might want to avoid using either Promises or Observables, and instead use a more traditional approach like callbacks or chaining Promises.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ultimately, the choice between a Promise and an Observable, or whether to use either one at all, will depend on the specific requirements of your application. It's important to understand the strengths and weaknesses of each concept, in order to make the right choice for your use case.&lt;/p&gt;

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

&lt;p&gt;Promises and Observables are two powerful concepts in JavaScript and TypeScript for handling asynchronous operations. Promises are used for representing a single asynchronous operation, where you just want to be notified when the operation is complete or has failed. Observables, on the other hand, are used for representing a sequence of asynchronous events, where you want to observe each event as it occurs over time.&lt;/p&gt;

&lt;p&gt;When choosing between Promises and Observables, it's important to consider the nature of the asynchronous operation you want to perform, and the requirements of your application. If you have a single asynchronous operation that you just need to perform, and you don't care about observing the results over time, a Promise is a good choice. If you have a sequence of events that you want to observe over time, an Observable is a good choice.&lt;/p&gt;

&lt;p&gt;In any case, it's important to understand the differences between Promises and Observables, and to make the right choice for your use case in order to write efficient, maintainable, and scalable code.&lt;/p&gt;

</description>
      <category>website</category>
      <category>documentation</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Why useeffect is running twice in react</title>
      <dc:creator>MD. JAHID HOSSAIN</dc:creator>
      <pubDate>Sat, 11 Feb 2023 11:48:47 +0000</pubDate>
      <link>https://forem.com/jahid6597/why-useeffect-is-running-twice-in-react-18c6</link>
      <guid>https://forem.com/jahid6597/why-useeffect-is-running-twice-in-react-18c6</guid>
      <description>&lt;p&gt;The &lt;code&gt;useEffect&lt;/code&gt; hook allows you to perform side effects in your React components. It takes a callback function as its first argument, which is called whenever the component is updated. The second argument is an array of dependencies, which is a list of values that the effect depends on. When any of these dependencies change, the effect is re-run. If the array is omitted or is empty, the effect will run on every render.&lt;/p&gt;

&lt;p&gt;In React, the &lt;code&gt;useEffect&lt;/code&gt; hook is called multiple times because it is designed to re-run whenever certain values (referred to as "dependencies") change.&lt;/p&gt;

&lt;p&gt;Here are a few reasons why an &lt;code&gt;useEffect&lt;/code&gt; might be called twice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Missing dependencies:&lt;/strong&gt; If you forget to include all of the values that the effect depends on, it will run twice. This is because it will re-run on every render, not just the ones where the data actually changed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Changing state inside the effect:&lt;/strong&gt; If you change the state inside an effect, it will cause a re-render, which will then cause the effect to re-run. To avoid this, make sure to wrap state updates inside a &lt;code&gt;useState&lt;/code&gt; or &lt;code&gt;useReducer&lt;/code&gt; call.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Async updates:&lt;/strong&gt; If you're updating state asynchronously (for example, in a &lt;code&gt;setTimeout&lt;/code&gt;), the state may change after the effect has already run, causing the effect to re-run on the next render.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to prevent your &lt;code&gt;useEffect&lt;/code&gt; from running twice, make sure to list all of its dependencies correctly and avoid changing state inside the effect.&lt;/p&gt;

&lt;p&gt;Here's an example of an &lt;code&gt;useEffect&lt;/code&gt; that causes unnecessary re-renders because of missing dependencies:&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;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&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="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Example&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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="kd"&gt;const&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="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="s2"&gt;`The count is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&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;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`The count is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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="nf"&gt;setCount&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Increase&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jane&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;Change&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;In this example, the &lt;code&gt;useEffect&lt;/code&gt; will run every time the component re-renders, even though the &lt;code&gt;count&lt;/code&gt; is the only thing that actually changed. To fix this, we can list &lt;code&gt;count&lt;/code&gt; as a dependency of the effect:&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;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&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="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Example&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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="kd"&gt;const&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="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="s2"&gt;`The count is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&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;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- Add the dependency here&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`The count is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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="nf"&gt;setCount&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Increase&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Jane&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;Change&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;With this change, the &lt;code&gt;useEffect&lt;/code&gt; will only run when the &lt;code&gt;count&lt;/code&gt; changes, and not every time the component re-renders.&lt;/p&gt;

&lt;p&gt;When using the &lt;code&gt;useEffect&lt;/code&gt; hook with an array of dependencies, you can use the &lt;code&gt;useRef&lt;/code&gt; hook to avoid unnecessary re-renders in some cases.&lt;/p&gt;

&lt;p&gt;For example, if you have an effect that depends on some values that don't change during the lifetime of your component, you can use a ref to store these values and exclude them from the list of dependencies. This way, the effect will only re-run when the values that actually matter change.&lt;/p&gt;

&lt;p&gt;Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useRef&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="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Example&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;countRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="s2"&gt;`The count is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;countRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Count: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;countRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&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;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;countRef&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`The count is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`The text is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;text&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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="nf"&gt;setCount&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Increase&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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="nf"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello again&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Change&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&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;In this example, we're using a ref to store the value of &lt;code&gt;count&lt;/code&gt; so that we can use it in the &lt;code&gt;useEffect&lt;/code&gt; hook. Since the ref is not a state value, updating it will not trigger a re-render, and the effect will only re-run when the value of &lt;code&gt;count&lt;/code&gt; actually changes.&lt;/p&gt;

&lt;p&gt;It's important to note that while &lt;code&gt;useRef&lt;/code&gt; can help you avoid unnecessary re-renders, it should be used carefully. Overusing &lt;code&gt;useRef&lt;/code&gt; can lead to complex and hard-to-debug code, so it's usually best to use state variables when possible.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>discuss</category>
    </item>
    <item>
      <title>API Architecture</title>
      <dc:creator>MD. JAHID HOSSAIN</dc:creator>
      <pubDate>Fri, 10 Feb 2023 10:33:55 +0000</pubDate>
      <link>https://forem.com/jahid6597/api-architecture-1h9p</link>
      <guid>https://forem.com/jahid6597/api-architecture-1h9p</guid>
      <description>&lt;h2&gt;
  
  
  What is API?
&lt;/h2&gt;

&lt;p&gt;API stands for "Application Programming Interface." It is a set of protocols, routines, and tools for building software and applications. An API specifies how software components should interact and APIs allow for communication between different systems and applications.&lt;/p&gt;

&lt;p&gt;In simple terms, an API acts as an intermediary between two applications, allowing them to exchange data and requests. For example, if you want to use information from Google Maps in your application, you can make use of the Google Maps API to request data, and the API will return the data to you in a format that you can use in your application.&lt;/p&gt;

&lt;p&gt;APIs are used in a variety of contexts, including web and mobile app development, cloud computing, and even hardware programming. They play a crucial role in allowing different systems to integrate and work together, making it easier for developers to build new and innovative applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  API Architecture Overview
&lt;/h2&gt;

&lt;p&gt;API (Application Programming Interface) architecture refers to the design and structure of APIs, which provide a way for different software systems to communicate with each other. It encompasses a set of protocols, routines, and tools for building software and applications, as well as defining the interaction between different components.&lt;/p&gt;

&lt;p&gt;A good API architecture should be easy to use and understand, efficient, scalable, secure, and flexible. It should also be consistent and well-documented, so that developers can easily integrate it into their systems.&lt;/p&gt;

&lt;p&gt;Some of the common elements of API architecture include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Endpoints:&lt;/strong&gt; A unique URL that represents a specific resource or collection of resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Requests and Responses:&lt;/strong&gt; The client makes a request to the API, which returns a response. The request typically includes information such as the endpoint, HTTP method, and any necessary parameters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HTTP Methods:&lt;/strong&gt; The standard methods used to interact with an API, such as &lt;strong&gt;&lt;code&gt;GET&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;POST&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;PUT&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;PATCH&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;DELETE&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Formats:&lt;/strong&gt; The format in which data is sent and received, such as &lt;strong&gt;&lt;code&gt;JSON&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;XML&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security:&lt;/strong&gt; Measures to ensure the confidentiality and integrity of data, such as authentication and encryption.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Versioning:&lt;/strong&gt; A way to maintain backward compatibility as the API evolves over time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error handling:&lt;/strong&gt; A way to handle and communicate errors that occur during API use.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The architecture of an API can have a significant impact on its success and how widely it is adopted. It's important to design APIs that are easy to use, efficient, and scalable, so that they can support the growing needs of the developers and businesses that rely on them.&lt;/p&gt;

&lt;p&gt;Let's take the example of an API for a weather service. The API allows developers to retrieve current weather information for a specific location.&lt;/p&gt;

&lt;p&gt;Here's an overview of the API architecture for this example:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Endpoints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;/weather&lt;/code&gt;:&lt;/strong&gt; Retrieves the current weather for a specific location.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Requests and Responses:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The client makes a &lt;strong&gt;&lt;code&gt;GET&lt;/code&gt;&lt;/strong&gt; request to the &lt;strong&gt;&lt;code&gt;/weather&lt;/code&gt;&lt;/strong&gt; endpoint, passing in the location as a parameter.&lt;/li&gt;
&lt;li&gt;The API returns a &lt;strong&gt;&lt;code&gt;JSON&lt;/code&gt;&lt;/strong&gt; response with the current temperature, humidity, and wind speed for the specified location.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. HTTP Methods:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;GET&lt;/code&gt;:&lt;/strong&gt; Used to retrieve information from the API.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;4. Data Formats:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JSON: Used to send and receive data between the client and the API.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. Security:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;API Key:&lt;/strong&gt; Developers must obtain an API key to access the service. This key is included in the request header to authenticate the client.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;6. Versioning:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The API version is specified in the URL, such as /v1/weather. This allows the API to evolve over time while maintaining backward compatibility for older clients.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;7. Error handling:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If there is an error with the request, such as an invalid location, the API returns an error message in the &lt;strong&gt;&lt;code&gt;JSON&lt;/code&gt;&lt;/strong&gt; response with a relevant error code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is just a basic example, but it illustrates the common elements of API architecture. By designing APIs with these best practices in mind, you can ensure that your API is easy to use, efficient, and scalable, which will encourage its wider adoption.&lt;/p&gt;

&lt;h2&gt;
  
  
  API architecture components
&lt;/h2&gt;

&lt;p&gt;API architecture can also include the following components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;API Gateway:&lt;/strong&gt; An API gateway is a component that sits in front of the API and acts as a reverse proxy, routing requests from clients to the appropriate API endpoint and managing security, caching, and rate limiting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API Portal:&lt;/strong&gt; An API portal is a web-based interface that provides documentation, developer support, and other resources for working with the API. An API portal can help simplify the development process and improve the user experience by providing a central location for all API-related information and resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Developer Sandbox:&lt;/strong&gt; A developer sandbox is a testing environment for developers to experiment with the API and build integrations. Sandboxes typically provide a simplified, isolated environment for testing, allowing developers to try out different scenarios and test their integrations without affecting the production environment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Analytics and Monitoring:&lt;/strong&gt; Analytics and monitoring tools can help track and analyze usage patterns, performance metrics, and error logs to help you optimize the performance and reliability of your API.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authentication and Authorization:&lt;/strong&gt; Authentication and authorization components are used to manage access control for the API. This may include implementing secure authentication protocols such as OAuth or JWT, as well as implementing fine-grained access control policies to control who can access what resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caching:&lt;/strong&gt; Caching can help improve the performance of your API by storing frequently requested data in a cache, reducing the number of trips to the backend database.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By combining these components in a well-designed API architecture, we can create a flexible, scalable, and secure API that delivers a great user experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to design an API Architecture?
&lt;/h2&gt;

&lt;p&gt;Designing an API architecture involves several key steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Determine the scope of the API:&lt;/strong&gt; What data or functionality do you want the API to expose? What are the requirements for the API, such as security and performance?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Choose the data format:&lt;/strong&gt; Decide on the format for sending and receiving data, such as &lt;strong&gt;&lt;code&gt;JSON&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;XML&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Define the endpoints:&lt;/strong&gt; Determine the specific &lt;strong&gt;&lt;code&gt;URLs&lt;/code&gt;&lt;/strong&gt; that will represent resources or collections of resources, and decide on the HTTP methods (&lt;strong&gt;&lt;code&gt;GET&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;POST&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;PUT&lt;/code&gt;&lt;/strong&gt;, etc.) that will be supported for each endpoint.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Design the request and response format:&lt;/strong&gt; Decide on the structure of the request and response data, including any parameters that need to be passed in the request, and any information that needs to be returned in the response.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Implement security measures:&lt;/strong&gt; Determine the necessary security measures for the API, such as API keys or OAuth, and implement these measures to ensure the confidentiality and integrity of data being exchanged.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Document the API:&lt;/strong&gt; Provide clear and comprehensive documentation for the API, including a description of each endpoint, the request and response format, and any security measures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test the API:&lt;/strong&gt; Test the API thoroughly to ensure that it is functioning as expected and to identify and fix any issues before it is deployed to production.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following these steps, we can design an API architecture that is effective, efficient, and easy to use. It is also important to keep the API architecture flexible and scalable, so that it can accommodate future changes and growth as needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Goals of API Design
&lt;/h2&gt;

&lt;p&gt;The goals of API design include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Usability:&lt;/strong&gt; The API should be easy to use and understand, with clear and concise documentation and straightforward endpoints and requests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexibility:&lt;/strong&gt; The API should be flexible enough to accommodate changes and growth over time, and should be able to support multiple data formats and devices.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consistency:&lt;/strong&gt; The API should be consistent in its structure and behavior, to ensure that it is predictable and easy to integrate with.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; The API should be fast and efficient, with minimal latency and maximum reliability.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security:&lt;/strong&gt; The API should be secure, with robust measures in place to protect the confidentiality and integrity of data being exchanged.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability:&lt;/strong&gt; The API should be scalable, to accommodate a large number of requests and users as needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Interoperability:&lt;/strong&gt; The API should be compatible with a wide range of systems and technologies, to facilitate easy integration and data exchange.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By focusing on these goals, we can design an API that meets the needs of its users and delivers a high-quality user experience. It is also important to continually evaluate and improve the API over time, to ensure that it remains effective and relevant as requirements and technologies change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Factors to consider when performing an API design
&lt;/h2&gt;

&lt;p&gt;When performing API design, there are several factors that should be considered to ensure that the API is well-designed, scalable, and secure. These factors include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;API Purpose and Requirements:&lt;/strong&gt; Understanding the purpose and requirements of the API is the first step in API design. This includes determining the types of data and operations that the API will provide, as well as the target audience and use cases for the API.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Models:&lt;/strong&gt; Designing effective data models is crucial for API design. The data model should be well-defined, consistent, and designed to support the needs of the API and its clients.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Endpoint Design:&lt;/strong&gt; Endpoint design is the process of defining the URIs or locations that clients will use to access the API and perform operations. Endpoints should be well-defined, consistent, and easy to use.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Request and Response Formats:&lt;/strong&gt; Defining the request and response formats for the API is critical for ensuring that the API is easy to use and well-understood. This includes defining standard and consistent request and response formats, as well as validation rules for requests and responses.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security:&lt;/strong&gt; Security is a critical consideration in API design. This includes implementing secure authentication protocols, such as OAuth or JWT, and implementing fine-grained access control policies to control who can access what resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; Performance is an important factor in API design, as it can affect the user experience and overall satisfaction with the API. This includes implementing caching, optimizing the API for speed and scalability, and monitoring performance metrics to identify and resolve performance bottlenecks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Error Handling:&lt;/strong&gt; Error handling is an important consideration in API design, as it can help ensure that the API is robust and reliable. This includes defining standard error codes, implementing error logging and reporting, and providing clear error messages to help clients understand and resolve issues.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Documentation:&lt;/strong&gt; Providing clear, concise, and up-to-date documentation is an important aspect of API design. This includes providing technical documentation for developers, as well as user-friendly documentation for end users.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By considering these factors and following best practices, you can design an API that is well-suited to the needs of your organization and your clients.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best practices when designing an API
&lt;/h2&gt;

&lt;p&gt;There are several best practices to follow when designing an API, including:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Keep it Simple:&lt;/strong&gt; API design should be simple and straightforward, with a clear and concise structure. Endpoints should be easy to understand and use, and data models should be well-defined and consistent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use Standards:&lt;/strong&gt; Where possible, API design should follow industry standards and best practices. This includes using standard protocols, such as HTTP and REST, and standard data formats, such as JSON.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Design for Evolution:&lt;/strong&gt; API design should be flexible and scalable, allowing for changes and additions over time. This includes designing for versioning, allowing for updates and changes to the API without affecting existing clients.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Document Thoroughly:&lt;/strong&gt; API design should include thorough documentation, including technical documentation for developers, user guides for end users, and examples and tutorials for both groups.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Be Consistent:&lt;/strong&gt; API design should be consistent, with a clear and consistent structure for endpoints, request and response formats, and error handling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ensure Security:&lt;/strong&gt; API design should include robust security measures, including secure authentication and authorization, as well as encryption of sensitive data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consider Performance:&lt;/strong&gt; API design should consider performance, including optimizing the API for speed and scalability, and implementing caching to reduce latency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitor and Test:&lt;/strong&gt; API design should include monitoring and testing to ensure that the API is working as expected and to identify and resolve any performance or security issues.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following these best practices, we can design an API that is simple, flexible, secure, and scalable, delivering a great user experience for our clients.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Architectural Styles for API Design
&lt;/h2&gt;

&lt;p&gt;There are several key architectural styles that can be used for API design, including:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Representational State Transfer (REST):&lt;/strong&gt; REST is a popular architectural style for designing web-based APIs. It uses standard HTTP methods, such as GET, POST, PUT, and DELETE, to perform operations on resources, and relies on a stateless, client-server architecture.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simple Object Access Protocol (SOAP):&lt;/strong&gt; SOAP is an older, XML-based protocol that was originally developed for exchanging structured data over the web. It is typically used in enterprise environments, and provides robust security and reliability features.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GraphQL:&lt;/strong&gt; GraphQL is a newer API architecture that provides a more efficient and flexible alternative to REST. It allows clients to request exactly the data they need, and to receive the data in a predictable format.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Remote Procedure Call (RPC):&lt;/strong&gt; RPC is an older, binary-based protocol that was originally used for inter-process communication on a single machine. It is still used in some contexts, and provides a fast and efficient way to call remote procedures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event-Driven Architecture (EDA):&lt;/strong&gt; EDA is an architectural style that is used for building highly scalable, real-time systems. It allows producers to send events to consumers, who can react to the events as needed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These architectural styles each have their own strengths and weaknesses, and the choice of style will depend on the specific requirements and constraints of the API. Some APIs may use a combination of styles, depending on the needs of different parts of the system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Layers of API Architecture
&lt;/h2&gt;

&lt;p&gt;The layers of API architecture can vary, but typically include the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Presentation Layer:&lt;/strong&gt; The presentation layer is responsible for receiving and processing requests from clients, and returning responses. This layer typically includes the API endpoints, request and response formats, and any necessary input validation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Business Logic Layer:&lt;/strong&gt; The business logic layer contains the core logic and functionality of the API, and implements the business rules and processes that govern data access and manipulation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Access Layer:&lt;/strong&gt; The data access layer is responsible for accessing and manipulating data in the underlying storage system, such as a database or data warehouse. This layer typically includes data access objects (DAOs), which are used to abstract the underlying data storage and provide a consistent interface to the business logic layer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Database Layer:&lt;/strong&gt; The database layer is the underlying storage system for the data, such as a relational database, NoSQL database, or data warehouse.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security Layer:&lt;/strong&gt; The security layer is responsible for implementing security measures, such as authentication, authorization, and encryption, to protect the confidentiality and integrity of the data being exchanged.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These layers can be organized in different ways, and the specific implementation will depend on the requirements and constraints of the API. However, by organizing the API into these distinct layers, you can improve maintainability, scalability, and security, and ensure that the API is easy to use and understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  API Architecture Best Practices For Deployment
&lt;/h2&gt;

&lt;p&gt;When deploying an API, it is important to follow best practices to ensure that the API is secure, scalable, and reliable. Some of the best practices for API architecture deployment include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Use versioning:&lt;/strong&gt; Versioning your API allows you to make changes to the API while preserving backward compatibility for existing clients. This can help you avoid breaking existing integrations and minimize the impact of changes on your users.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use a load balancer:&lt;/strong&gt; Using a load balancer can help distribute incoming requests evenly across multiple API servers, improving performance and reliability. Load balancers also provide features such as SSL offloading, caching, and request routing, which can help simplify the deployment process.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use a web server or reverse proxy:&lt;/strong&gt; A web server or reverse proxy can help protect your API servers from direct access and improve security by handling SSL encryption, caching, and request routing. It can also provide additional functionality such as request logging, rate limiting, and IP blocking.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use API gateway:&lt;/strong&gt; An API gateway can provide centralized management and security for your API, allowing you to enforce policies and manage authentication and authorization. An API gateway can also provide features such as caching, request routing, and rate limiting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitor and log API activity:&lt;/strong&gt; Monitoring and logging API activity can help you identify performance issues, diagnose errors, and track usage patterns. This can help you optimize performance and troubleshoot problems more effectively.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automate deployment:&lt;/strong&gt; Automating the deployment process can help you quickly and efficiently deploy new versions of your API, reducing the risk of errors and downtime. Automation can also help you enforce consistency and ensure that all API servers are configured identically.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Implement API security:&lt;/strong&gt; Implementing API security is critical for protecting the confidentiality and integrity of data and ensuring the API is not exploited for malicious purposes. Best practices for API security include using encryption, authentication and authorization, and rate limiting.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following these best practices, we can create an API architecture that is scalable, secure, and reliable, and that delivers a high-quality user experience. Regularly reviewing and updating your API architecture is essential to ensuring it remains effective and relevant over time.&lt;/p&gt;

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

&lt;p&gt;API architecture is a critical aspect of software development that defines the structure and organization of APIs. A well-designed API architecture can improve the performance, scalability, security, and overall success of an API. The components of an API architecture include the API gateway, portal, server, and client. Effective API architecture requires considering factors such as the use case, target audience, data models, and security. By following best practices, organizations can design an API architecture that meets their needs and delivers great user experiences to their clients. Ultimately, an effective API architecture is a key ingredient in building successful and sustainable applications.&lt;/p&gt;

</description>
      <category>api</category>
      <category>webdev</category>
      <category>discuss</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
