Explore Observer Pattern JavaScript | How To Implement

The Observer Pattern is a widely used design pattern in the realm of software development, particularly in JavaScript, that fosters better code organization, flexibility, and maintainability.

In this article, we’ll delve into the concepts, benefits, and implementation of the Observer Pattern in JavaScript. So, let’s dive right in!

What is Observer Pattern JavaScript?

At its core, the observer pattern is a behavioral design pattern that facilitates efficient communication between different parts of a software application.

It establishes a relationship between objects— when one object undergoes a change, all its dependent objects are automatically notified and updated.

This ensures a loose coupling between components, enabling flexible and modular code.

Two Actors of Observer Pattern

The observer pattern consists of two main actors: the subject and the observers.

The subject is often observable it is the entity that experiences changes. On the other hand, observers are entities that are interested in these changes and wish to be notified when they occur.

Javascript Observer

Benefits of Using the Observer Pattern

The JavaScript Observers bring a lot of benefits to the table namely:

  1. Modularity and Reusability — Through decoupling components, we can modify, add, or remove observers without impacting other observers.
  2. Efficient Communication — Observers are only notified when relevant changes occur, minimizing unnecessary updates.
  3. Flexibility — It considers dynamic scenarios wherein the number of observers can change during run time.
  4. Maintainability — The pattern enhances code organization and readability by isolating responsibilities.

Observer Pattern Javascript Step by Step Implementation

Now, here is the step-by-step implementation of the Observer Pattern in JavaScript.

Step 1: Define the Subject

Start by creating the subject, which is the object that experiences changes and notifies its observers.

Then, define the class that represents the subjects and includes methods for managing observers and notifying the changes.

In this case, we create Stock class with methods to set stock price along with notifying the observers when the price changes.

class Stock {
  constructor(symbol, initialPrice) {
    this.symbol = symbol;
    this.price = initialPrice;
    this.observers = [];
  }

  setPrice(newPrice) {
    this.price = newPrice;
    this.notifyObservers();
  }

  registerObserver(observer) {
    this.observers.push(observer);
  }

  unregisterObserver(observer) {
    const index = this.observers.indexOf(observer);
    if (index !== -1) {
      this.observers.splice(index, 1);
    }
  }

  notifyObservers() {
    this.observers.forEach(observer => observer.update(this.price));
  }
}

Step 2: Implement the Observer Interface

Create an observer interface that outlines the methods observers must implement. Typically, this interface includes update() method which is called when the subject’s state changes.

class Observer {
  update(price) {
    throw new Error("This method must be overridden");
  }
}

Step 3: Create Concrete Observer Classes

Develop concrete observer classes that implement the observer interface. Each observer class represents a specific component that is interested in the subject’s changes.

These classes should include logic to react to updates received from the subject.

For instance, develop PriceDisplay and GraphDisplay.

class PriceDisplay extends Observer {
  constructor(stock) {
    super();
    this.stock = stock;
  }

  update(price) {
    console.log(`PriceDisplay: Stock ${this.stock.symbol} has a new price: ${price}`);
  }
}

class GraphDisplay extends Observer {
  constructor(stock) {
    super();
    this.stock = stock;
  }

  update(price) {
    console.log(`GraphDisplay: Updating graph for stock ${this.stock.symbol} with price ${price}`);
  }
}

Step 4: Register Observers with the Subject

In the subject class, provide methods to register and unregister observers. Maintain a collection of observers and ensure that they are notified whenever changes occur.

In the Stock class, maintain a list of registered observers. When a stock’s price changes, notify all registered observers.

const stock = new Stock("XYZ", 100);

const priceDisplay = new PriceDisplay(stock);
const graphDisplay = new GraphDisplay(stock);

stock.registerObserver(priceDisplay);
stock.registerObserver(graphDisplay);

stock.setPrice(110);

Step 5: Notify Observers

When the subject’s state changes, iterate through the list of registered observers and call their update() methods. This allows each observer to react to the change in its own way.

const stock = new Stock("ABC", 200);

const priceDisplay = new PriceDisplay(stock);
const graphDisplay = new GraphDisplay(stock);

stock.registerObserver(priceDisplay);
stock.registerObserver(graphDisplay);

stock.setPrice(220);

Step 6: Client Code

In your application’s client code, instantiate the subject and the observer classes. Register the observers with the subject, and start monitoring changes.

This completes the setup, and your components are now connected through the observer pattern.

For instance, we instantiate the Stock object and the observer instances. Register observers with the subject and watch for updates on different displays.

const stock = new Stock("ABC", 200);

const priceDisplay = new PriceDisplay(stock);
const graphDisplay = new GraphDisplay(stock);

stock.registerObserver(priceDisplay);
stock.registerObserver(graphDisplay);

stock.setPrice(220);

Conclusion

In conclusion, the Observer Pattern shines as a valuable technique for achieving loose coupling, real-time updates, and code maintainability.

By understanding its core concepts, benefits, and best practices, developers can wield this pattern to create more robust and adaptable applications.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Leave a Comment