Skip to content
jshacks.dev
Patterns

Do Design Patterns Still Matter in JavaScript Today

Design patterns are named solutions to recurring design problems. See which classic patterns JavaScript absorbed and which still earn their place today.

6 min read
Summarize in AI
Flat illustration of a clock over a calendar of scheduled tasks, with the JavaScript, TypeScript, npm, Node.js and Vite logos.

A design pattern is a named, reusable solution to a problem that keeps showing up in software design. The classic catalogue comes from the 1994 book by the "Gang of Four", written for C++ and Smalltalk, and many of its patterns exist to work around limits of those languages: no first-class functions, no closures, no dynamic objects. JavaScript has all three, so people ask whether the patterns are obsolete. The honest answer is that some are, some are built into the language and some are still the clearest way to talk about a design problem. This post explains what a pattern is, sorts a dozen well-known patterns into those three groups with short code, and gives a rule for when naming a pattern helps and when it adds ceremony. The examples run on Node.js 24.

What a pattern is, and what it is not

A pattern has three parts: a recurring problem, a solution shape and the trade-offs of that shape. It is not a library, a template to copy or a rule. The value of the name is communication. "This is an observer" tells a teammate how the pieces relate in two words, and they can predict the places where it will hurt, such as forgotten unsubscribes. A pattern applied where the problem does not exist is just extra code.

Patterns also depend on the language. A solution that needs a class hierarchy in Java may be a function in JavaScript. That is the main reason to ask the question, and the answer is not "patterns are dead" but "look at what the language already gives you before you build the structure."

Patterns the language has absorbed

Some patterns exist in JavaScript as plain features, so building them by hand is usually a mistake.

Iterator. The pattern separates traversal from the collection. JavaScript has it as the iteration protocol, with Symbol.iterator, generators and for...of.

iterator.mjsJavaScript
class Countdown {
  constructor(from) {
    this.from = from;
  }
  *[Symbol.iterator]() {
    for (let n = this.from; n > 0; n--) yield n;
  }
}

console.log([...new Countdown(3)]);
Text
[ 3, 2, 1 ]

Strategy. The pattern swaps an algorithm at runtime. In class-based languages it needs an interface and several classes. In JavaScript, you pass a function.

strategy.mjsJavaScript
const byPrice = (a, b) => a.price - b.price;
const byName = (a, b) => a.name.localeCompare(b.name);

const items = [
  { name: "pear", price: 3 },
  { name: "apple", price: 5 },
];

console.log(items.toSorted(byPrice).map((i) => i.name));
console.log(items.toSorted(byName).map((i) => i.name));
Text
[ 'pear', 'apple' ]
[ 'apple', 'pear' ]

The comparison functions are the strategies, and toSorted is the context. No class was needed, which is typical: first-class functions absorb Strategy, Command, Template Method and most of Visitor's boilerplate.

Singleton. An ES module is evaluated once and cached, so a module that exports an object is a singleton without any pattern code. The caveat is that two copies of a package in node_modules produce two instances.

Decorator (the wrapping kind). Wrapping a function in another function that adds behavior is ordinary higher-order function use, as in a withLogging(fn) helper. Decorator syntax for classes is a separate language proposal, supported by TypeScript, and should not be confused with the pattern.

Patterns that are still useful as they are

Several patterns solve problems that no language feature removes, and they appear in every serious codebase.

Observer. One object notifies many others when something changes. Node.js ships it as EventEmitter, and browsers ship it as addEventListener. You rarely write it from scratch, but you use it constantly, and the pattern name tells you about its hazard: subscribers that are never removed.

observer.mjsJavaScript
import { EventEmitter } from "node:events";

class Cart extends EventEmitter {
  #items = [];
  add(item) {
    this.#items.push(item);
    this.emit("changed", this.#items.length);
  }
}

const cart = new Cart();
cart.on("changed", (count) => console.log("items:", count));
cart.add("book");
cart.add("pen");
Text
items: 1
items: 2

Factory. A function that creates objects, hiding which concrete type or setup is used. In JavaScript it is just a function that returns an object, and it is valuable whenever creation involves choices or asynchronous setup, such as createServer(options) or a static Client.create() that awaits configuration.

Adapter. Wrapping an interface so it matches the one your code expects. This is extremely common when you integrate a third-party library or a legacy API, and the code is usually a small object or function that translates names and shapes.

Proxy. An object that stands in for another and controls access. JavaScript has a built-in Proxy, which makes the pattern easy, and it is the foundation of reactivity systems in some frameworks.

proxy.mjsJavaScript
const user = { name: "Ada" };
const logged = new Proxy(user, {
  get(target, key) {
    console.log("read", String(key));
    return target[key];
  },
});

console.log(logged.name);
Text
read name
Ada

Facade, Mediator and Builder also keep their value: a simple entry point over a complicated subsystem, a coordinator that stops components talking to each other directly, and a stepwise construction of a complex object. They are design ideas more than syntax workarounds, so the language does not replace them.

Patterns that are often overused

The pattern most often misapplied in JavaScript is the one with the most ceremony. Abstract Factory, Visitor with double dispatch and deep inheritance hierarchies were designed for statically typed class languages, and in JavaScript they usually arrive as a heavy structure around something a function and an object literal would do. Singleton in the classic "private constructor and static instance" form is another: with modules it adds nothing, and global mutable state is the real problem, as it makes testing hard.

A telling sign of overuse is a class with a single method and no state. If a class exists only to carry one function, make it a function. Another is an interface with one implementation and no plan for a second. Add the abstraction when the second case appears, not before, a point developed in YAGNI.

A rule for deciding

Ask three questions. Is the problem real, meaning you have at least two cases that differ in the way the pattern handles, or a concrete change you expect? Does the language or platform already provide it, as with iterators, events and modules? And does the pattern make the code easier for the next reader, or only for someone who has read the catalogue? If the answer to the first is no, skip the pattern. If the second is yes, use the built-in. If the third is no, simplify.

When a pattern does apply, use its name in code and comments: createUserFactory, PaymentAdapter, onChange observers. Teams that share the vocabulary review faster, because "this is a facade over three services" replaces a paragraph of explanation.

Where the advice stops

None of this means you can skip learning the catalogue. Patterns teach you to see structure: where responsibilities should split, where change is likely and what coupling costs. Developers who know them recognize the same shape in React hooks, Express middleware and database repositories. The risk is the reverse, forcing a design into a pattern name before checking that the problem exists. Treat patterns as vocabulary, not as a checklist.

Share

Keep reading

Flat illustration of two options weighed side by side, with the JavaScript, Vite and Node.js logos.
Functions

Classes vs Factory Functions

Classes vs factory functions in JavaScript compared on private state, this, instanceof, inheritance, memory, JSON and testing, with exact output.

7 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.