🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
JS MASTER CLASS /// MASTER THE ENGINE /// BUILD LOGIC /// ASYNC PATTERNS /// JS MASTER CLASS /// MASTER THE ENGINE ///

The Observer Pattern | JavaScript Tutorial - In-Depth Guide

Master the Observer Pattern: subjects that maintain a list of observers, notifying them on state changes, implementing subscribe/unsubscribe, and its relationship to the DOM's native event system.

Total XP: 0|💻 javascript XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

System Hub

Core logic.

Quick Quiz //

Do all observers subscribed to the same subject need to react to a notification in exactly the same way?


🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.

The Observer Pattern lets one or more "observer" objects automatically react whenever a "subject" object's state changes — the foundational idea behind DOM events, reactive state libraries, and much of modern UI programming.

1The Observer Pattern | JavaScript Tutorial - In-Depth Guide Part 1

In the Observer Pattern, a 'subject' maintains a list of 'observers' and notifies all of them whenever its state changes, without needing to know anything specific about what each observer does.

+
class Subject {
  observers = [];
  subscribe(observer) { this.observers.push(observer); }
  notify(data) { this.observers.forEach((obs) => obs.update(data)); }
}
localhost:3000
👀

Subjects and Observers

2The Observer Pattern | JavaScript Tutorial - In-Depth Guide Part 2

Each observer implements a common interface (like an 'update' method) so the subject can notify any of them identically, regardless of what each one actually does in response.

+
const loggingObserver = { update: (data) => console.log('Changed:', data) };
const uiObserver = { update: (data) => renderUI(data) };
subject.subscribe(loggingObserver);
subject.subscribe(uiObserver);
localhost:3000

A Common Observer Interface

3The Observer Pattern | JavaScript Tutorial - In-Depth Guide Part 3

A production-quality implementation also supports unsubscribing, so an observer that's no longer needed (like a removed UI component) can stop receiving notifications.

+
class Subject {
  observers = [];
  subscribe(observer) {
    this.observers.push(observer);
    return () => { // returns an unsubscribe function
      this.observers = this.observers.filter((o) => o !== observer);
    };
  }
}
localhost:3000

Supporting Unsubscribe

4The Observer Pattern | JavaScript Tutorial - In-Depth Guide Part 4

The browser's native DOM event system ('addEventListener') is itself a real-world implementation of the Observer Pattern: elements are subjects, and registered listener functions are observers.

+
// This IS the Observer Pattern:
button.addEventListener('click', handleClick); // subscribe
button.removeEventListener('click', handleClick); // unsubscribe
localhost:3000

DOM Events Are Observer Pattern

5The Observer Pattern | JavaScript Tutorial - In-Depth Guide Part 5

Reactive state libraries (like those behind modern frontend frameworks) use the Observer Pattern internally: a piece of state is the subject, and UI components that read it are observers automatically re-rendered when it changes.

+
// Conceptually similar to what a reactive state library does internally:
const state = createObservableState({ count: 0 });
state.subscribe((newState) => rerenderComponent(newState));
localhost:3000

Powering Reactive UI Libraries

6Step-by-Step Breakdown

In the Observer Pattern, a 'subject' maintains a list of 'observers' and notifies all of them whenever its state changes, without needing to know anything specific about what each observer does.

Each observer implements a common interface (like an 'update' method) so the subject can notify any of them identically, regardless of what each one actually does in response.

Checkpoint: Do all observers subscribed to the same subject need to react to a notification in exactly the same way?

  • Yes, every observer must implement identical behavior
  • No, each observer can react however it needs to

A production-quality implementation also supports unsubscribing, so an observer that's no longer needed (like a removed UI component) can stop receiving notifications.

The browser's native DOM event system ('addEventListener') is itself a real-world implementation of the Observer Pattern: elements are subjects, and registered listener functions are observers.

Checkpoint: Is the DOM's addEventListener/removeEventListener mechanism an example of the Observer Pattern?

  • Yes, elements are subjects and listeners are observers
  • No, they are conceptually unrelated

Reactive state libraries (like those behind modern frontend frameworks) use the Observer Pattern internally: a piece of state is the subject, and UI components that read it are observers automatically re-rendered when it changes.

Next, we'll explore 'Publish / Subscribe'.

Level Up 🚀

Advanced cheat sheets, SEO tricks, and interview prep for this topic.

Browser Support

ChromeSupported

Fully supported.

FirefoxSupported

Fully supported.

SafariSupported

Fully supported.

EdgeSupported

Fully supported.

Accessibility (A11y)

1Use the Observer Pattern to Keep ARIA Live Regions in Sync with State Changes

Subscribing a dedicated 'accessibility announcer' observer to a state subject ensures that whenever relevant state changes (like a new notification arriving), the corresponding ARIA live region is updated consistently, in the same place, regardless of how many other UI observers also react to that same state change.

SEO Implications

  • 1

    No Direct SEO Effect

    The Observer Pattern is an application-architecture concept; SEO relevance is limited to enabling more maintainable, bug-resistant reactive UI code.

Best Practices

Always Provide and Use an Unsubscribe Mechanism

Without it, subjects accumulate references to observers that should have been removed, creating a memory leak pattern identical to forgotten event listeners.

Keep the Observer Interface Simple and Consistent

A single, uniform method (like update()) that every observer implements keeps the subject's notification logic simple and lets observers be added/removed freely without special-casing.

Frequent Bugs

THE BUG

A subject accumulates subscribed observers indefinitely because unsubscribing was never implemented or never called, causing notify() to grow slower and referenced observers to leak memory over time.

THE FIX

Implement and consistently call an unsubscribe function (or removeObserver method) whenever an observer is no longer needed, such as when a UI component unmounts.

THE BUG

An observer's update() method throws an error, which (depending on implementation) can halt the notify() loop and prevent subsequent observers from being notified at all.

THE FIX

Wrap each observer's update() call in a try/catch inside the notify loop, so one misbehaving observer doesn't prevent others from receiving their notification.

Real-World Examples

A Simple State Container with Subscribable Updates

A small app needed a central piece of state that multiple, independent UI components could subscribe to and automatically react to when it changed.

function createStore(initialState) {
  let state = initialState;
  const observers = new Set();
  return {
    getState: () => state,
    setState(newState) {
      state = { ...state, ...newState };
      observers.forEach((obs) => obs(state));
    },
    subscribe(obs) {
      observers.add(obs);
      return () => observers.delete(obs);
    },
  };
}

Interview Prep

?Frequently Asked Questions

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Observers accumulating indefinitely with no way to unsubscribe

const unsubscribe = subject.subscribe(observer); // later: unsubscribe();

The Solution //

Implement subscribe() to return an unsubscribe function, and call it during cleanup.

Lesson Glossary

[01]Observer Pattern

A pattern where a subject notifies a list of observers whenever its state changes.

Code Preview
subject.subscribe(observer)

[02]Subject

The object being observed, responsible for maintaining and notifying its list of observers.

Code Preview
subject.notify(data)

[03]Observer

An object reacting to notifications from a subject, typically via a common interface method.

Code Preview
observer.update(data)

[04]Unsubscribe

Removing an observer from a subject's notification list, preventing further updates and memory leaks.

Code Preview
subject.unsubscribe(observer)

[05]Reactive State

State whose readers (observers) are automatically notified and updated when the state changes.

Code Preview
observable state

Continue Learning