<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arun Teja</title>
    <description>The latest articles on DEV Community by Arun Teja (@arunteja).</description>
    <link>https://dev.to/arunteja</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%2F75672%2F8d9ba322-fc05-4bc5-8fad-2cbf0a8aba59.png</url>
      <title>DEV Community: Arun Teja</title>
      <link>https://dev.to/arunteja</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arunteja"/>
    <language>en</language>
    <item>
      <title>Singleton vs Observer Pattern: When and Why to Use Each</title>
      <dc:creator>Arun Teja</dc:creator>
      <pubDate>Sun, 11 Jan 2026 15:12:06 +0000</pubDate>
      <link>https://dev.to/arunteja/singleton-vs-observer-pattern-when-and-why-to-use-each-43k3</link>
      <guid>https://dev.to/arunteja/singleton-vs-observer-pattern-when-and-why-to-use-each-43k3</guid>
      <description>&lt;p&gt;Design patterns are not meant to be memorized—they are meant to be &lt;strong&gt;chosen&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Two patterns that are often mentioned together, yet solve &lt;strong&gt;very different problems&lt;/strong&gt;, are the &lt;strong&gt;Singleton Pattern&lt;/strong&gt; and the &lt;strong&gt;Observer Pattern&lt;/strong&gt;. Beginners sometimes confuse them because both are commonly used in global or shared contexts.&lt;/p&gt;

&lt;p&gt;In this article, we’ll clearly compare these two patterns, understand &lt;strong&gt;what problem each one solves&lt;/strong&gt;, and learn &lt;strong&gt;when to use which&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you haven’t read them yet, you may want to start here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/arunteja/singleton-pattern-explained-simply-with-javascript-examples-13a0"&gt;Singleton Pattern Explained Simply&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/arunteja/observer-pattern-explained-simply-with-javascript-examples-6fe"&gt;Observer Pattern Explained Simply&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Why Comparing Singleton and Observer Makes Sense
&lt;/h2&gt;

&lt;p&gt;At first glance, Singleton and Observer may seem unrelated:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One controls &lt;strong&gt;how many instances exist&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;The other controls &lt;strong&gt;how objects communicate&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, in real-world systems, these two patterns are often used &lt;strong&gt;together&lt;/strong&gt;, which is why understanding their differences is important.&lt;/p&gt;

&lt;p&gt;The key distinction lies in &lt;strong&gt;what they are responsible for&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Core Problem Each Pattern Solves
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Singleton Pattern: Object Lifecycle
&lt;/h3&gt;

&lt;p&gt;The Singleton Pattern answers the question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;How many instances of this object should exist?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Its responsibility is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Controlling object creation&lt;/li&gt;
&lt;li&gt;Ensuring a single shared instance&lt;/li&gt;
&lt;li&gt;Providing a global access point&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It focuses on &lt;strong&gt;existence and ownership&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Observer Pattern: Object Communication
&lt;/h3&gt;

&lt;p&gt;The Observer Pattern answers a different question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;How do multiple parts of the system react when something changes?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Its responsibility is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Managing subscriptions&lt;/li&gt;
&lt;li&gt;Notifying observers&lt;/li&gt;
&lt;li&gt;Keeping components loosely coupled&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It focuses on &lt;strong&gt;events and reactions&lt;/strong&gt;, not creation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Lifecycle vs Communication (The Key Difference)
&lt;/h2&gt;

&lt;p&gt;This single comparison explains almost everything:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Aspect&lt;/th&gt;
&lt;th&gt;Singleton&lt;/th&gt;
&lt;th&gt;Observer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Primary concern&lt;/td&gt;
&lt;td&gt;Object creation&lt;/td&gt;
&lt;td&gt;Object communication&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Core question&lt;/td&gt;
&lt;td&gt;“How many instances?”&lt;/td&gt;
&lt;td&gt;“Who should react?”&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Focus&lt;/td&gt;
&lt;td&gt;Lifecycle&lt;/td&gt;
&lt;td&gt;Events&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Relationship&lt;/td&gt;
&lt;td&gt;One instance&lt;/td&gt;
&lt;td&gt;One-to-many&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Coupling&lt;/td&gt;
&lt;td&gt;Global access&lt;/td&gt;
&lt;td&gt;Loose coupling&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you remember just this table, you’ll rarely confuse the two again.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Responsibility Comparison
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Singleton Responsibility
&lt;/h3&gt;

&lt;p&gt;A Singleton:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Decides &lt;strong&gt;when and how&lt;/strong&gt; an object is created&lt;/li&gt;
&lt;li&gt;Ensures the same instance is reused&lt;/li&gt;
&lt;li&gt;Does &lt;strong&gt;not&lt;/strong&gt; handle communication logic by itself&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Logger instance&lt;/li&gt;
&lt;li&gt;Database connection&lt;/li&gt;
&lt;li&gt;Configuration loader&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Observer Responsibility
&lt;/h3&gt;

&lt;p&gt;An Observer system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manages &lt;strong&gt;subscriptions&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Decides &lt;strong&gt;who gets notified&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Does &lt;strong&gt;not&lt;/strong&gt; care how observers are created&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Event listeners&lt;/li&gt;
&lt;li&gt;UI updates&lt;/li&gt;
&lt;li&gt;Notification systems&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Can Singleton and Observer Be Used Together?
&lt;/h2&gt;

&lt;p&gt;Yes—very often.&lt;/p&gt;

&lt;p&gt;In fact, many real systems combine them naturally.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;NotificationService&lt;/strong&gt; exists as a Singleton&lt;/li&gt;
&lt;li&gt;Multiple components subscribe to it as Observers&lt;/li&gt;
&lt;li&gt;The service publishes events&lt;/li&gt;
&lt;li&gt;Observers react independently&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Singleton ensures &lt;strong&gt;one notification hub&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Observer ensures &lt;strong&gt;many listeners&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They solve &lt;strong&gt;different layers of the problem&lt;/strong&gt; and complement each other.&lt;/p&gt;




&lt;h2&gt;
  
  
  When to Use Singleton vs Observer
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use Singleton When:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You need exactly &lt;strong&gt;one instance&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;The object represents a &lt;strong&gt;shared resource&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Multiple instances would cause conflicts&lt;/li&gt;
&lt;li&gt;The concern is &lt;strong&gt;global consistency&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Use Observer When:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Multiple parts of the system must react to changes&lt;/li&gt;
&lt;li&gt;You want to avoid tight coupling&lt;/li&gt;
&lt;li&gt;Events should propagate automatically&lt;/li&gt;
&lt;li&gt;The system is event-driven&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Use Both When:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You have a central service (Singleton)&lt;/li&gt;
&lt;li&gt;That service emits events&lt;/li&gt;
&lt;li&gt;Multiple independent consumers need updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This combination is extremely common in real-world applications.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Misconceptions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ❌ “Singleton notifies observers”
&lt;/h3&gt;

&lt;p&gt;No. Singleton controls instance creation, not notifications.&lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ “Observer is global”
&lt;/h3&gt;

&lt;p&gt;No. Observers are scoped to a subject, not global by default.&lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ “They are alternatives”
&lt;/h3&gt;

&lt;p&gt;They are &lt;strong&gt;orthogonal patterns&lt;/strong&gt;, not replacements for each other.&lt;/p&gt;




&lt;h2&gt;
  
  
  Decision Cheat Sheet
&lt;/h2&gt;

&lt;p&gt;Ask yourself:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Am I solving an object creation problem?&lt;/em&gt;&lt;br&gt;&lt;br&gt;
→ &lt;strong&gt;Singleton&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Am I solving a notification or event problem?&lt;/em&gt;&lt;br&gt;&lt;br&gt;
→ &lt;strong&gt;Observer&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Do I need one shared service that many things react to?&lt;/em&gt;&lt;br&gt;&lt;br&gt;
→ &lt;strong&gt;Singleton + Observer&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;The Singleton and Observer patterns solve &lt;strong&gt;completely different problems&lt;/strong&gt;, but they often appear together in well-designed systems.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Singleton brings &lt;strong&gt;control&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Observer brings &lt;strong&gt;flexibility&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding &lt;strong&gt;what each pattern is responsible for&lt;/strong&gt; is far more important than memorizing their definitions.&lt;/p&gt;

&lt;p&gt;If you choose patterns based on &lt;strong&gt;problems, not popularity&lt;/strong&gt;, your system design will naturally improve.&lt;/p&gt;




&lt;h3&gt;
  
  
  Series Links
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;📘 &lt;a href="https://dev.to/arunteja/singleton-pattern-explained-simply-with-javascript-examples-13a0"&gt;Singleton Pattern Explained Simply&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📗 &lt;a href="https://dev.to/arunteja/observer-pattern-explained-simply-with-javascript-examples-6fe"&gt;Observer Pattern Explained Simply&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>architecture</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Singleton vs Observer Pattern: When and Why to Use Each</title>
      <dc:creator>Arun Teja</dc:creator>
      <pubDate>Sun, 11 Jan 2026 15:12:06 +0000</pubDate>
      <link>https://dev.to/arunteja/singleton-vs-observer-pattern-when-and-why-to-use-each-26jj</link>
      <guid>https://dev.to/arunteja/singleton-vs-observer-pattern-when-and-why-to-use-each-26jj</guid>
      <description>&lt;p&gt;Design patterns are not meant to be memorized—they are meant to be &lt;strong&gt;chosen&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Two patterns that are often mentioned together, yet solve &lt;strong&gt;very different problems&lt;/strong&gt;, are the &lt;strong&gt;Singleton Pattern&lt;/strong&gt; and the &lt;strong&gt;Observer Pattern&lt;/strong&gt;. Beginners sometimes confuse them because both are commonly used in global or shared contexts.&lt;/p&gt;

&lt;p&gt;In this article, we’ll clearly compare these two patterns, understand &lt;strong&gt;what problem each one solves&lt;/strong&gt;, and learn &lt;strong&gt;when to use which&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you haven’t read them yet, you may want to start here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/arunteja/singleton-pattern-explained-simply-with-javascript-examples-13a0"&gt;Singleton Pattern Explained Simply&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/arunteja/observer-pattern-explained-simply-with-javascript-examples-6fe"&gt;Observer Pattern Explained Simply&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Why Comparing Singleton and Observer Makes Sense
&lt;/h2&gt;

&lt;p&gt;At first glance, Singleton and Observer may seem unrelated:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One controls &lt;strong&gt;how many instances exist&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;The other controls &lt;strong&gt;how objects communicate&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, in real-world systems, these two patterns are often used &lt;strong&gt;together&lt;/strong&gt;, which is why understanding their differences is important.&lt;/p&gt;

&lt;p&gt;The key distinction lies in &lt;strong&gt;what they are responsible for&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Core Problem Each Pattern Solves
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Singleton Pattern: Object Lifecycle
&lt;/h3&gt;

&lt;p&gt;The Singleton Pattern answers the question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;How many instances of this object should exist?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Its responsibility is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Controlling object creation&lt;/li&gt;
&lt;li&gt;Ensuring a single shared instance&lt;/li&gt;
&lt;li&gt;Providing a global access point&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It focuses on &lt;strong&gt;existence and ownership&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Observer Pattern: Object Communication
&lt;/h3&gt;

&lt;p&gt;The Observer Pattern answers a different question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;How do multiple parts of the system react when something changes?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Its responsibility is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Managing subscriptions&lt;/li&gt;
&lt;li&gt;Notifying observers&lt;/li&gt;
&lt;li&gt;Keeping components loosely coupled&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It focuses on &lt;strong&gt;events and reactions&lt;/strong&gt;, not creation.&lt;/p&gt;




&lt;h2&gt;
  
  
  Lifecycle vs Communication (The Key Difference)
&lt;/h2&gt;

&lt;p&gt;This single comparison explains almost everything:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Aspect&lt;/th&gt;
&lt;th&gt;Singleton&lt;/th&gt;
&lt;th&gt;Observer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Primary concern&lt;/td&gt;
&lt;td&gt;Object creation&lt;/td&gt;
&lt;td&gt;Object communication&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Core question&lt;/td&gt;
&lt;td&gt;“How many instances?”&lt;/td&gt;
&lt;td&gt;“Who should react?”&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Focus&lt;/td&gt;
&lt;td&gt;Lifecycle&lt;/td&gt;
&lt;td&gt;Events&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Relationship&lt;/td&gt;
&lt;td&gt;One instance&lt;/td&gt;
&lt;td&gt;One-to-many&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Coupling&lt;/td&gt;
&lt;td&gt;Global access&lt;/td&gt;
&lt;td&gt;Loose coupling&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you remember just this table, you’ll rarely confuse the two again.&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Responsibility Comparison
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Singleton Responsibility
&lt;/h3&gt;

&lt;p&gt;A Singleton:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Decides &lt;strong&gt;when and how&lt;/strong&gt; an object is created&lt;/li&gt;
&lt;li&gt;Ensures the same instance is reused&lt;/li&gt;
&lt;li&gt;Does &lt;strong&gt;not&lt;/strong&gt; handle communication logic by itself&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Logger instance&lt;/li&gt;
&lt;li&gt;Database connection&lt;/li&gt;
&lt;li&gt;Configuration loader&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Observer Responsibility
&lt;/h3&gt;

&lt;p&gt;An Observer system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manages &lt;strong&gt;subscriptions&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Decides &lt;strong&gt;who gets notified&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Does &lt;strong&gt;not&lt;/strong&gt; care how observers are created&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Event listeners&lt;/li&gt;
&lt;li&gt;UI updates&lt;/li&gt;
&lt;li&gt;Notification systems&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Can Singleton and Observer Be Used Together?
&lt;/h2&gt;

&lt;p&gt;Yes—very often.&lt;/p&gt;

&lt;p&gt;In fact, many real systems combine them naturally.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;NotificationService&lt;/strong&gt; exists as a Singleton&lt;/li&gt;
&lt;li&gt;Multiple components subscribe to it as Observers&lt;/li&gt;
&lt;li&gt;The service publishes events&lt;/li&gt;
&lt;li&gt;Observers react independently&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Singleton ensures &lt;strong&gt;one notification hub&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Observer ensures &lt;strong&gt;many listeners&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They solve &lt;strong&gt;different layers of the problem&lt;/strong&gt; and complement each other.&lt;/p&gt;




&lt;h2&gt;
  
  
  When to Use Singleton vs Observer
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use Singleton When:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You need exactly &lt;strong&gt;one instance&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;The object represents a &lt;strong&gt;shared resource&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Multiple instances would cause conflicts&lt;/li&gt;
&lt;li&gt;The concern is &lt;strong&gt;global consistency&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Use Observer When:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Multiple parts of the system must react to changes&lt;/li&gt;
&lt;li&gt;You want to avoid tight coupling&lt;/li&gt;
&lt;li&gt;Events should propagate automatically&lt;/li&gt;
&lt;li&gt;The system is event-driven&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Use Both When:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You have a central service (Singleton)&lt;/li&gt;
&lt;li&gt;That service emits events&lt;/li&gt;
&lt;li&gt;Multiple independent consumers need updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This combination is extremely common in real-world applications.&lt;/p&gt;




&lt;h2&gt;
  
  
  Common Misconceptions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ❌ “Singleton notifies observers”
&lt;/h3&gt;

&lt;p&gt;No. Singleton controls instance creation, not notifications.&lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ “Observer is global”
&lt;/h3&gt;

&lt;p&gt;No. Observers are scoped to a subject, not global by default.&lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ “They are alternatives”
&lt;/h3&gt;

&lt;p&gt;They are &lt;strong&gt;orthogonal patterns&lt;/strong&gt;, not replacements for each other.&lt;/p&gt;




&lt;h2&gt;
  
  
  Decision Cheat Sheet
&lt;/h2&gt;

&lt;p&gt;Ask yourself:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Am I solving an object creation problem?&lt;/em&gt;&lt;br&gt;&lt;br&gt;
→ &lt;strong&gt;Singleton&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Am I solving a notification or event problem?&lt;/em&gt;&lt;br&gt;&lt;br&gt;
→ &lt;strong&gt;Observer&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Do I need one shared service that many things react to?&lt;/em&gt;&lt;br&gt;&lt;br&gt;
→ &lt;strong&gt;Singleton + Observer&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;The Singleton and Observer patterns solve &lt;strong&gt;completely different problems&lt;/strong&gt;, but they often appear together in well-designed systems.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Singleton brings &lt;strong&gt;control&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Observer brings &lt;strong&gt;flexibility&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding &lt;strong&gt;what each pattern is responsible for&lt;/strong&gt; is far more important than memorizing their definitions.&lt;/p&gt;

&lt;p&gt;If you choose patterns based on &lt;strong&gt;problems, not popularity&lt;/strong&gt;, your system design will naturally improve.&lt;/p&gt;




&lt;h3&gt;
  
  
  Series Links
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;📘 &lt;a href="https://dev.to/arunteja/singleton-pattern-explained-simply-with-javascript-examples-13a0"&gt;Singleton Pattern Explained Simply&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📗 &lt;a href="https://dev.to/arunteja/observer-pattern-explained-simply-with-javascript-examples-6fe"&gt;Observer Pattern Explained Simply&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>architecture</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Observer Pattern Explained Simply With JavaScript Examples</title>
      <dc:creator>Arun Teja</dc:creator>
      <pubDate>Sun, 11 Jan 2026 15:03:46 +0000</pubDate>
      <link>https://dev.to/arunteja/observer-pattern-explained-simply-with-javascript-examples-6fe</link>
      <guid>https://dev.to/arunteja/observer-pattern-explained-simply-with-javascript-examples-6fe</guid>
      <description>&lt;p&gt;Modern applications are highly interactive and event-driven.&lt;br&gt;&lt;br&gt;
When one part of a system changes, many other parts often need to react to that change.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Observer Pattern&lt;/strong&gt; exists to solve this exact problem—&lt;strong&gt;how to notify multiple parts of a system when something changes, without tightly coupling them together&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this article, we’ll understand the Observer Pattern using clear explanations, a real-world perspective, and a practical JavaScript example.&lt;/p&gt;




&lt;h2&gt;
  
  
  Core Values &amp;amp; Definition
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Core Value
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Loose coupling and reactive communication.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Observer Pattern focuses on &lt;strong&gt;how objects communicate&lt;/strong&gt;, not how they are created.&lt;/p&gt;

&lt;p&gt;It answers an important question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;How can multiple objects react to a change without constantly checking for updates or being tightly linked to each other?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;strong&gt;Observer Pattern&lt;/strong&gt; defines a one-to-many relationship where multiple objects (observers) subscribe to another object (subject) and are automatically notified when the subject’s state changes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In simpler terms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One object changes&lt;/li&gt;
&lt;li&gt;Many others react&lt;/li&gt;
&lt;li&gt;No direct dependency between them&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Main Features
&lt;/h2&gt;

&lt;p&gt;The key characteristics of the Observer Pattern are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One &lt;strong&gt;Subject&lt;/strong&gt; maintains a list of observers&lt;/li&gt;
&lt;li&gt;Multiple &lt;strong&gt;Observers&lt;/strong&gt; subscribe to the subject&lt;/li&gt;
&lt;li&gt;Observers are &lt;strong&gt;notified automatically&lt;/strong&gt; on state change&lt;/li&gt;
&lt;li&gt;Observers can be added or removed dynamically&lt;/li&gt;
&lt;li&gt;Promotes &lt;strong&gt;loose coupling&lt;/strong&gt; between components&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These features make Observer ideal for event-driven systems.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Real-World Perspective
&lt;/h2&gt;

&lt;p&gt;Think about subscribing to a YouTube channel.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You subscribe to a channel&lt;/li&gt;
&lt;li&gt;When the creator uploads a new video, you get notified&lt;/li&gt;
&lt;li&gt;You don’t constantly check the channel for updates&lt;/li&gt;
&lt;li&gt;You can unsubscribe anytime&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The channel doesn’t know &lt;em&gt;who you are&lt;/em&gt;—it just notifies all subscribers when something changes.&lt;/p&gt;

&lt;p&gt;That’s exactly how the Observer Pattern works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One subject&lt;/li&gt;
&lt;li&gt;Many observers&lt;/li&gt;
&lt;li&gt;Automatic notifications&lt;/li&gt;
&lt;li&gt;No tight dependencies&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Practical Exercise
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Scenario: Simple Notification System
&lt;/h3&gt;

&lt;p&gt;Imagine an application where multiple components need to react when a new message arrives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UI updates&lt;/li&gt;
&lt;li&gt;Email notifications&lt;/li&gt;
&lt;li&gt;Logging&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hard-coding calls to each component would tightly couple the system and make it difficult to extend.&lt;/p&gt;

&lt;p&gt;Observer solves this cleanly.&lt;/p&gt;




&lt;h3&gt;
  
  
  JavaScript Implementation
&lt;/h3&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;NotificationService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;unsubscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribers&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;sub&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;sub&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sub&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;sub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserNotifier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;update&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;User notified:&lt;/span&gt;&lt;span class="dl"&gt;"&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailNotifier&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;update&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Email sent:&lt;/span&gt;&lt;span class="dl"&gt;"&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="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Usage&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;notificationService&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;NotificationService&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;userNotifier&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;UserNotifier&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;emailNotifier&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;EmailNotifier&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;notificationService&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;userNotifier&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;notificationService&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;emailNotifier&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;notificationService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;New message received&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;h3&gt;
  
  
  What This Example Demonstrates
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;One &lt;strong&gt;subject&lt;/strong&gt; manages a list of observers&lt;/li&gt;
&lt;li&gt;Observers react automatically when the subject changes&lt;/li&gt;
&lt;li&gt;New observers can be added without changing existing logic&lt;/li&gt;
&lt;li&gt;Observers remain loosely coupled to the subject&lt;/li&gt;
&lt;li&gt;The system becomes flexible and easy to extend&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This example highlights the core strength of the Observer Pattern:&lt;br&gt;&lt;br&gt;
&lt;strong&gt;changes in one part of the system automatically propagate to others without tight coupling&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real-World Use Cases
&lt;/h2&gt;

&lt;p&gt;The Observer Pattern is commonly used in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Event systems (click, scroll, resize)&lt;/li&gt;
&lt;li&gt;UI frameworks (React, Angular, Vue)&lt;/li&gt;
&lt;li&gt;Real-time dashboards&lt;/li&gt;
&lt;li&gt;Notification systems&lt;/li&gt;
&lt;li&gt;Chat and messaging applications&lt;/li&gt;
&lt;li&gt;State management libraries&lt;/li&gt;
&lt;li&gt;Node.js &lt;code&gt;EventEmitter&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whenever something happens and multiple parts of the system need to react, Observer is a natural fit.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Identify When to Use Observer
&lt;/h2&gt;

&lt;p&gt;Use the Observer Pattern when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple components need to react to a single change&lt;/li&gt;
&lt;li&gt;You want to avoid tight coupling between components&lt;/li&gt;
&lt;li&gt;Observers may be added or removed dynamically&lt;/li&gt;
&lt;li&gt;Automatic updates are preferred over manual polling&lt;/li&gt;
&lt;li&gt;The system is event-driven by nature&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A helpful question to ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Do multiple independent parts of my system need to react when something changes?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the answer is &lt;strong&gt;yes&lt;/strong&gt;, the Observer Pattern is a strong candidate.&lt;/p&gt;




&lt;h3&gt;
  
  
  When NOT to Use Observer
&lt;/h3&gt;

&lt;p&gt;Avoid the Observer Pattern when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only a single consumer exists&lt;/li&gt;
&lt;li&gt;Simple function calls are sufficient&lt;/li&gt;
&lt;li&gt;Execution order is critical and complex&lt;/li&gt;
&lt;li&gt;Too many notifications would add unnecessary overhead&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Observer should not replace straightforward communication unnecessarily.&lt;/p&gt;




&lt;h2&gt;
  
  
  Disadvantages and Trade-offs
&lt;/h2&gt;

&lt;p&gt;Like any design pattern, Observer comes with trade-offs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Common Issues
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Harder debugging due to many observers&lt;/li&gt;
&lt;li&gt;Unexpected side effects from poorly designed observers&lt;/li&gt;
&lt;li&gt;Memory leaks if observers are not unsubscribed&lt;/li&gt;
&lt;li&gt;Notification order may be unpredictable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These issues often surface in large or unstructured systems.&lt;/p&gt;




&lt;h3&gt;
  
  
  How to Mitigate These Issues
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Clearly define observer responsibilities&lt;/li&gt;
&lt;li&gt;Always unsubscribe observers when they are no longer needed&lt;/li&gt;
&lt;li&gt;Avoid heavy business logic inside observers&lt;/li&gt;
&lt;li&gt;Keep event flows simple and well-documented&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When used thoughtfully, Observer remains powerful and maintainable.&lt;/p&gt;




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

&lt;p&gt;The Observer Pattern is a foundation of modern, reactive software systems.&lt;/p&gt;

&lt;p&gt;Used correctly, it enables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Loose coupling&lt;/li&gt;
&lt;li&gt;Scalable communication&lt;/li&gt;
&lt;li&gt;Clean event-driven architecture&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Used carelessly, it can introduce:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Event sprawl&lt;/li&gt;
&lt;li&gt;Debugging complexity&lt;/li&gt;
&lt;li&gt;Hidden dependencies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The key is &lt;strong&gt;clarity and discipline&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When multiple parts of a system need to react to change without knowing about each other, the Observer Pattern is the right tool.&lt;/p&gt;

</description>
      <category>designpatterns</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Singleton Pattern Explained Simply With JavaScript Examples</title>
      <dc:creator>Arun Teja</dc:creator>
      <pubDate>Sun, 11 Jan 2026 14:19:29 +0000</pubDate>
      <link>https://dev.to/arunteja/singleton-pattern-explained-simply-with-javascript-examples-13a0</link>
      <guid>https://dev.to/arunteja/singleton-pattern-explained-simply-with-javascript-examples-13a0</guid>
      <description>&lt;p&gt;Design patterns often feel abstract until we clearly see the problem they solve.&lt;br&gt;&lt;br&gt;
The &lt;strong&gt;Singleton Pattern&lt;/strong&gt; is one of the simplest &amp;amp; important design patterns, yet it is frequently misunderstood and misused.&lt;/p&gt;

&lt;p&gt;In this article, we’ll break down the Singleton Pattern using clear explanations, a relatable real-world perspective, and a practical JavaScript example that shows &lt;em&gt;why&lt;/em&gt; this pattern exists.&lt;/p&gt;




&lt;h2&gt;
  
  
  Core Values &amp;amp; Definition
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Core Value
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Control and consistency.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Singleton Pattern exists to answer one important question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;How do we ensure that only one instance of an object exists across the entire application?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Some objects represent shared resources. Creating multiple instances of such objects can lead to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inconsistent state&lt;/li&gt;
&lt;li&gt;Conflicting behavior&lt;/li&gt;
&lt;li&gt;Difficult debugging&lt;/li&gt;
&lt;li&gt;Unnecessary memory usage&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Definition
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;strong&gt;Singleton Pattern&lt;/strong&gt; ensures that a class or object has &lt;strong&gt;only one instance&lt;/strong&gt; and provides a &lt;strong&gt;global access point&lt;/strong&gt; to that instance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In simpler terms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Object creation is controlled&lt;/li&gt;
&lt;li&gt;Everyone works with the same instance&lt;/li&gt;
&lt;li&gt;The application behaves consistently&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Main Features
&lt;/h2&gt;

&lt;p&gt;A typical Singleton has the following characteristics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Single instance&lt;/strong&gt; throughout the application lifecycle&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Global access point&lt;/strong&gt; to that instance&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Controlled creation logic&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Shared state&lt;/strong&gt; across all consumers&lt;/li&gt;
&lt;li&gt;Often &lt;strong&gt;lazy initialization&lt;/strong&gt; (created only when needed)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  A Real-World Perspective
&lt;/h2&gt;

&lt;p&gt;Think about the music player on your phone.&lt;/p&gt;

&lt;p&gt;You can control it from multiple places:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lock screen&lt;/li&gt;
&lt;li&gt;Notification panel&lt;/li&gt;
&lt;li&gt;Music app itself&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But no matter where you press &lt;em&gt;play&lt;/em&gt; or &lt;em&gt;pause&lt;/em&gt;, there is only &lt;strong&gt;one active music player&lt;/strong&gt; running in the background.&lt;/p&gt;

&lt;p&gt;If your phone created a new music player instance every time you interacted with a control:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multiple songs would play at once&lt;/li&gt;
&lt;li&gt;Volume controls would conflict&lt;/li&gt;
&lt;li&gt;The entire experience would break&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To avoid this, the system ensures that all controls talk to the &lt;strong&gt;same underlying player instance&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That is exactly how the Singleton Pattern works:&lt;br&gt;
one shared instance, accessed from many places, behaving consistently.&lt;/p&gt;




&lt;h2&gt;
  
  
  Practical Exercise
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Scenario: Centralized Application Logger
&lt;/h3&gt;

&lt;p&gt;In real-world applications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Logs are generated from many different files&lt;/li&gt;
&lt;li&gt;Logs should be collected in one place&lt;/li&gt;
&lt;li&gt;Log order and consistency are important for debugging&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Creating multiple logger instances would:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fragment logs&lt;/li&gt;
&lt;li&gt;Break ordering&lt;/li&gt;
&lt;li&gt;Make debugging harder&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A logger is a perfect candidate for the Singleton Pattern.&lt;/p&gt;




&lt;h3&gt;
  
  
  JavaScript Implementation
&lt;/h3&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;Logger&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;instance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="nx"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;entry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`[&lt;/span&gt;&lt;span class="p"&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="nf"&gt;toISOString&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="nx"&gt;message&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;entry&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;entry&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;getLogs&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logs&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="c1"&gt;// Usage&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;logger1&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;Logger&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;logger2&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;Logger&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;logger1&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;Application started&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;logger2&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;User logged in&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;logger1&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;logger2&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;h3&gt;
  
  
  What This Example Demonstrates
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Only &lt;strong&gt;one logger instance&lt;/strong&gt; exists across the application&lt;/li&gt;
&lt;li&gt;Logs coming from different parts of the code end up in the &lt;strong&gt;same place&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Shared state is intentional and meaningful&lt;/li&gt;
&lt;li&gt;The need for Singleton is clear and practical, not theoretical&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This example shows why Singleton is useful when &lt;strong&gt;multiple instances would actively cause problems&lt;/strong&gt;, not just inconvenience.&lt;/p&gt;




&lt;h2&gt;
  
  
  Real-World Use Cases
&lt;/h2&gt;

&lt;p&gt;The Singleton Pattern is commonly used for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Logger services&lt;/li&gt;
&lt;li&gt;Database connection managers&lt;/li&gt;
&lt;li&gt;Cache managers&lt;/li&gt;
&lt;li&gt;Message queue clients (Kafka, RabbitMQ)&lt;/li&gt;
&lt;li&gt;Feature flag systems&lt;/li&gt;
&lt;li&gt;Application-wide configuration loaders&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are &lt;strong&gt;shared infrastructure components&lt;/strong&gt; that should remain consistent across the entire application.&lt;/p&gt;




&lt;h2&gt;
  
  
  How to Identify When to Use Singleton
&lt;/h2&gt;

&lt;p&gt;Use the Singleton Pattern when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need &lt;strong&gt;exactly one instance&lt;/strong&gt; of an object&lt;/li&gt;
&lt;li&gt;The object represents a &lt;strong&gt;shared resource&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Multiple instances would lead to conflicts or inconsistency&lt;/li&gt;
&lt;li&gt;Consistency across the application is critical&lt;/li&gt;
&lt;li&gt;Object creation is expensive or resource-heavy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A helpful question to ask yourself is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Does it make sense for this object to exist more than once?&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If the answer is &lt;strong&gt;no&lt;/strong&gt;, Singleton is a strong candidate.&lt;/p&gt;




&lt;h3&gt;
  
  
  When NOT to Use Singleton
&lt;/h3&gt;

&lt;p&gt;Avoid the Singleton Pattern when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need multiple independent instances&lt;/li&gt;
&lt;li&gt;Behavior should vary per consumer&lt;/li&gt;
&lt;li&gt;Test isolation is important&lt;/li&gt;
&lt;li&gt;Global state would increase coupling between modules&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Singleton should be used &lt;strong&gt;deliberately&lt;/strong&gt;, not by default.&lt;/p&gt;




&lt;h2&gt;
  
  
  Disadvantages and Trade-offs
&lt;/h2&gt;

&lt;p&gt;Like any design pattern, Singleton comes with trade-offs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Common Issues
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Behaves like &lt;strong&gt;global state&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Makes unit testing harder&lt;/li&gt;
&lt;li&gt;Introduces hidden dependencies&lt;/li&gt;
&lt;li&gt;Can be easily overused or abused&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These issues usually arise when Singleton is used outside of infrastructure-level concerns.&lt;/p&gt;




&lt;h3&gt;
  
  
  How to Mitigate These Issues
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Keep Singleton logic minimal and focused&lt;/li&gt;
&lt;li&gt;Avoid placing business logic inside Singleton classes&lt;/li&gt;
&lt;li&gt;Prefer &lt;strong&gt;dependency injection&lt;/strong&gt; where possible&lt;/li&gt;
&lt;li&gt;Use Singleton mainly for shared infrastructure components&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Used carefully, these practices reduce the downsides significantly.&lt;/p&gt;




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

&lt;p&gt;The Singleton Pattern is neither good nor bad—it is a &lt;strong&gt;tool&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Used correctly, it provides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Order&lt;/li&gt;
&lt;li&gt;Consistency&lt;/li&gt;
&lt;li&gt;Predictability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Used carelessly, it can introduce:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tight coupling&lt;/li&gt;
&lt;li&gt;Testing complexity&lt;/li&gt;
&lt;li&gt;Hidden dependencies&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The key is &lt;strong&gt;intentional usage&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When a system genuinely needs &lt;strong&gt;one and only one instance&lt;/strong&gt;, the Singleton Pattern is the right choice.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>designpatterns</category>
      <category>programming</category>
    </item>
    <item>
      <title>Mastering JavaScript Set Operations: 7 Powerful Methods You Can’t Miss</title>
      <dc:creator>Arun Teja</dc:creator>
      <pubDate>Sat, 14 Jun 2025 06:48:40 +0000</pubDate>
      <link>https://dev.to/arunteja/mastering-javascript-set-operations-7-powerful-methods-you-cant-miss-4b4c</link>
      <guid>https://dev.to/arunteja/mastering-javascript-set-operations-7-powerful-methods-you-cant-miss-4b4c</guid>
      <description>&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%2Fzrcxehssp4glu21upjer.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%2Fzrcxehssp4glu21upjer.png" alt="Image description" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;p&gt;Learn how JavaScript’s seven new Set methods—&lt;code&gt;difference&lt;/code&gt;, &lt;code&gt;intersection&lt;/code&gt;, &lt;code&gt;union&lt;/code&gt;, &lt;code&gt;symmetricDifference&lt;/code&gt;, &lt;code&gt;isDisjointFrom&lt;/code&gt;, &lt;code&gt;isSubsetOf&lt;/code&gt;, and &lt;code&gt;isSupersetOf&lt;/code&gt;—let you write cleaner, more expressive code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In ES6, the Set object became a staple for storing unique values, but until recently, combining or comparing sets required clunky loops or array conversions. You might have written code like this to get the intersection of two sets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const intersection = new Set(
  [...setA].filter(x =&amp;gt; setB.has(x))
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thankfully, JavaScript now includes built-in methods that make these operations concise and self-documenting. In this article, we’ll explore seven powerful Set methods—&lt;code&gt;difference()&lt;/code&gt;, &lt;code&gt;intersection()&lt;/code&gt;, &lt;code&gt;union()&lt;/code&gt;, &lt;code&gt;symmetricDifference()&lt;/code&gt;, &lt;code&gt;isDisjointFrom()&lt;/code&gt;, &lt;code&gt;isSubsetOf()&lt;/code&gt;, and &lt;code&gt;isSupersetOf()&lt;/code&gt;—to help you refactor old code and simplify your data-structure logic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deep Dive: The 7 New Set Methods
&lt;/h2&gt;

&lt;p&gt;For each method below you’ll see:&lt;br&gt;
    1.  Signature&lt;br&gt;
    2.  What it does&lt;br&gt;
    3.  Example&lt;br&gt;
    4.  When to use it&lt;/p&gt;
&lt;h2&gt;
  
  
  1. difference()
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set.prototype.difference(otherSet) → Set
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;br&gt;
Returns a new Set containing all values in the original set that are &lt;strong&gt;not&lt;/strong&gt; in otherSet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setA = new Set([1, 2, 3, 4]);
const setB = new Set([3, 4, 5]);
const onlyInA = setA.difference(setB);
console.log(onlyInA); // Set {1, 2}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;When to use it&lt;/strong&gt;
Filtering out unwanted values from one collection based on another.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  2. intersection()
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set.prototype.intersection(otherSet) → Set
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;br&gt;
Returns a new Set containing only the values found in &lt;strong&gt;both&lt;/strong&gt; sets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setA = new Set(['apple', 'banana', 'cherry']);
const setB = new Set(['banana', 'dragonfruit', 'apple']);
const common = setA.intersection(setB);
console.log(common); // Set {'apple', 'banana'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;When to use it&lt;/strong&gt;
Finding overlaps between two datasets (e.g., shared tags, common permissions).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  3. union()
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set.prototype.union(otherSet) → Set
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;br&gt;
Returns a new Set containing &lt;strong&gt;all unique&lt;/strong&gt; values from both sets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setA = new Set([1, 2]);
const setB = new Set([2, 3, 4]);
const all = setA.union(setB);
console.log(all); // Set {1, 2, 3, 4}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;When to use it&lt;/strong&gt;
Merging data streams or combining feature-flag sets.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  4. symmetricDifference()
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set.prototype.symmetricDifference(otherSet) → Set
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;br&gt;
Returns a new Set of values that are in &lt;strong&gt;either&lt;/strong&gt; set but &lt;strong&gt;not both&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setA = new Set(['x', 'y', 'z']);
const setB = new Set(['y', 'w']);
const diff = setA.symmetricDifference(setB);
console.log(diff); // Set {'x', 'z', 'w'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;When to use it&lt;/strong&gt;
Identifying elements that differ between two configurations or snapshots.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  5. isDisjointFrom()
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set.prototype.isDisjointFrom(otherSet) → boolean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;br&gt;
Returns &lt;code&gt;true&lt;/code&gt; if the two sets share &lt;strong&gt;no&lt;/strong&gt; common elements.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const setA = new Set([10, 20]);
const setB = new Set([30, 40]);
console.log(setA.isDisjointFrom(setB)); // true

const setC = new Set([20, 50]);
console.log(setA.isDisjointFrom(setC)); // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;When to use it&lt;/strong&gt;
Quickly check whether two collections overlap at all (e.g., scheduling conflicts).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  6. isSubsetOf()
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set.prototype.isSubsetOf(otherSet) → boolean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;br&gt;
Returns &lt;code&gt;true&lt;/code&gt; if &lt;strong&gt;every&lt;/strong&gt; element in the original set is also in otherSet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const small = new Set([1, 2]);
const large = new Set([1, 2, 3]);
console.log(small.isSubsetOf(large)); // true

const weird = new Set([2, 4]);
console.log(weird.isSubsetOf(large)); // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;When to use it&lt;/strong&gt;
Validating permission hierarchies or ensuring one dataset is contained in another.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  7. isSupersetOf()
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Set.prototype.isSupersetOf(otherSet) → boolean
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;What it does:&lt;/strong&gt;&lt;br&gt;
Returns &lt;code&gt;true&lt;/code&gt; if the original set contains &lt;strong&gt;every&lt;/strong&gt; element of otherSet.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const roles = new Set(['admin', 'editor', 'viewer']);
const needed = new Set(['editor', 'viewer']);
console.log(roles.isSupersetOf(needed)); // true

const extra = new Set(['editor', 'contributor']);
console.log(roles.isSupersetOf(extra)); // false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;When to use it&lt;/strong&gt;
Checking that a user’s permissions include all required rights for a given action.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Practical Use Cases
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1.De-duplicating Combined Data Sources&lt;/strong&gt;&lt;br&gt;
When you merge results from multiple APIs or database queries, you often need to remove duplicates. Instead of mapping to arrays and back, use union() directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const apiA = new Set(await fetchUsersFromServiceA());
const apiB = new Set(await fetchUsersFromServiceB());
const uniqueUsers = apiA.union(apiB);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2.Permission and Role Checks&lt;/strong&gt;&lt;br&gt;
Model user roles or feature toggles as sets, then use isSupersetOf() to verify access:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const userRoles = new Set(['reader', 'commenter']);
const required   = new Set(['reader']);
if (!userRoles.isSupersetOf(required)) {
  throw new Error('Insufficient permissions');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3.Comparing Configurations or Feature Flags&lt;/strong&gt;&lt;br&gt;
Quickly detect what settings have changed between two snapshots using symmetricDifference():&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const oldFlags = new Set(['beta', 'darkMode']);
const newFlags = new Set(['darkMode', 'logging']);
const changed  = oldFlags.symmetricDifference(newFlags);
console.log(changed); // Set {'beta', 'logging'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4.Filtering Out Unwanted Items&lt;/strong&gt;&lt;br&gt;
Remove deprecated IDs or blacklisted entries with difference():&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const allItems     = new Set(getAllItemIds());
const deprecated   = new Set(getDeprecatedIds());
const activeItems  = allItems.difference(deprecated);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;5.Overlap and Conflict Detection&lt;/strong&gt;&lt;br&gt;
Use &lt;code&gt;isDisjointFrom()&lt;/code&gt; to verify that two schedules, seat assignments, or resource pools don’t collide:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const morningSlots = new Set(['09:00', '10:00', '11:00']);
const bookedSlots  = new Set(['11:00', '12:00']);
if (!morningSlots.isDisjointFrom(bookedSlots)) {
  console.log('Conflict at 11:00');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






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

&lt;p&gt;JavaScript’s new Set methods—&lt;code&gt;difference()&lt;/code&gt;, &lt;code&gt;intersection()&lt;/code&gt;, &lt;code&gt;union()&lt;/code&gt;, &lt;code&gt;symmetricDifference()&lt;/code&gt;, &lt;code&gt;isDisjointFrom()&lt;/code&gt;&lt;br&gt;
, &lt;code&gt;isSubsetOf()&lt;/code&gt;, and &lt;code&gt;isSupersetOf()&lt;/code&gt;—turn once-verbose patterns into clear, intent-driven calls.&lt;/p&gt;

&lt;p&gt;Now that these operations are part of the language, it’s a great time to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Revisit your old code that relied on array hacks or manual loops&lt;/li&gt;
&lt;li&gt;Refactor your data-handling logic to use these self-documenting methods&lt;/li&gt;
&lt;li&gt;Share your 
favorite use cases and tips in the comments below!&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>frontend</category>
      <category>learning</category>
    </item>
    <item>
      <title>Vim Tabs</title>
      <dc:creator>Arun Teja</dc:creator>
      <pubDate>Mon, 26 Jul 2021 19:06:19 +0000</pubDate>
      <link>https://dev.to/arunteja/vim-tabs-2m4j</link>
      <guid>https://dev.to/arunteja/vim-tabs-2m4j</guid>
      <description>&lt;p&gt;Just like any edit, we open can open multiples files as tab in vim as well. Let's see how it is done.&lt;/p&gt;

&lt;p&gt;As usual open a file in vim as &lt;code&gt;vim index.html&lt;/code&gt; after the file is opened, from command mode enter &lt;code&gt;:tabedit nameOfTheFile&lt;/code&gt;, you want open in an other tab. This way we can open as many files we want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example:
   :tabedit style.css
   :tabedit script.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or their as another alternative way to open multiple files in tab&lt;br&gt;
&lt;code&gt;vim -p firstFileName secondFileName thirdFileName...&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Example:
    vim -p index.html style.css script.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After opening multiple files in tabs, to navigate among them we use &lt;code&gt;gt&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gt will take us to the next tab
gT will take us to the previous tab
Or we can also got a specific tab, by providing the tab number
Example:
    2gt will take us to the second tab.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;:quitall&lt;/code&gt; in command mode, closes all the tabs &amp;amp; vim as well.&lt;/p&gt;

&lt;p&gt;That's it for today, It's just introduction to the tabs in vim. If have missed anything or you wanted to add anything, please feel free comment them below. Thanks for reading.&lt;/p&gt;

</description>
      <category>vim</category>
      <category>beginners</category>
      <category>editor</category>
    </item>
  </channel>
</rss>
