Early previewThis guide is in development. Items tagged verify are still being checked against official docs.
nebx Learn
GitHub
Menu
Home/JavaScript/Anti-patterns
JS

JavaScript

The language of the web, on the client, the server and the edge.

Paradigm
Multi-paradigm, prototype-based, functional
Typing
Dynamic, weak (TypeScript adds static types)
Memory
Garbage collected
Runs
V8 / SpiderMonkey / JSC engines (JIT)
Concurrency
Single-threaded event loop, workers

Anti-patterns

Solutions that look reasonable but cause problems. First the JavaScript-specific traps, then the classics that apply everywhere.

JavaScript-specific

Anti-pattern
Do this instead
✕ Callback pyramids
✓ Use async / await; flatten error handling into try/catch.
✕ Loose equality and implicit coercion
✓ Use === and convert types explicitly (Number(x), String(x)).
✕ Blocking the event loop with CPU work
✓ Move heavy computation to worker threads or a separate service.
✕ Extending built-in prototypes
✓ Write utility functions or classes; monkey-patching Array.prototype breaks other code.
✕ Shared mutable state across modules
✓ Prefer pure functions and explicit state containers.

Universal anti-patterns

God object

One class knows and does too much. Split it by responsibility (SRP).

Spaghetti code

Tangled control flow with no structure. Refactor into small functions with clear inputs and outputs.

Golden hammer

Using one familiar tool or pattern for every problem. Pick the simplest fit.

Premature optimisation

Optimising before measuring. Profile first, then fix the real bottleneck.

Copy-paste programming

Duplicated logic drifts apart. Extract shared code once the duplication is real (rule of three).

Magic numbers and strings

Unexplained literals. Use named constants or enums.

Singleton abuse

Global state disguised as a pattern. Prefer dependency injection with a single-instance lifetime.

Lava flow

Dead or experimental code nobody dares to remove. Delete it; version control remembers.

Saved in this browser only. Part of From code to production (0/5).