Design Patterns

Functional Programming

Functional Programming treats computation as evaluation of pure mathematical functions. Learn pure functions, immutability, higher-order functions, currying, function composition, and memoization — and see how they power React, Redux, and modern JavaScript.

What is functional programming in JavaScript?

Functional programming builds programs from small pure functions, which return the same output for the same input and have no side effects, and treats data as immutable. In JavaScript this mostly means using map, filter and reduce instead of loops that mutate, returning new objects instead of changing old ones, and composing small functions into bigger ones.

const users = [{ name: "Ada", active: true }, { name: "Bob", active: false }];

const activeNames = users
  .filter((u) => u.active)
  .map((u) => u.name);                    // ["Ada"]

const total = cart.reduce((sum, item) => sum + item.price, 0);
const pipe = (...fns) => (x) => fns.reduce((v, f) => f(v), x);

FP Concepts Quick Reference

ConceptDefinitionJS Example
Pure functionSame input → same output, no side effectsconst add = (a, b) => a + b
ImmutabilityNever mutate; return new data{ ...obj, key: newVal }
First-class fnFunctions are values you can pass/returnconst fn = x => x * 2
Higher-order fnFunction that takes or returns a function.map(), .filter(), .reduce()
Curryingf(a, b) → f(a)(b) — partial applicationconst add = a => b => a + b
CompositionChain f(g(x)) — small fns → big transformspipe(trim, lower, slugify)
Referential transparencyExpression can be replaced by its valuememoize enables this
FunctorContainer you can map over safelyArray, Promise, Maybe

Common Pitfalls

Creating huge intermediate arrays with long .filter().map().filter().map() chains
Use a single .reduce() for complex pipelines, or process data in one pass. Transducers eliminate intermediate arrays entirely.
Over-currying everything — adds ceremony without benefit for simple 2-argument functions
Curry only when partial application is genuinely useful (reusable predicates, piped transforms). Plain functions are fine otherwise.
Mutating arrays inside .map()/.filter() — breaks purity and causes bugs with React state
Always return new values: map returns new array, spread creates new objects. Never push/splice inside functional chains.
Deeply composing too many tiny functions — unreadable 10-function pipe chains
Group related transforms into named functions: const formatName = pipe(trim, lower). Balance composability with readability.

Interactive Demo

Code Editor

<pre id="out"></pre>

Preview

Functional programming — step by step

1 / 4
1

Pure functions — the foundation

A pure function always returns the same output for the same inputs and produces no observable side effects (no mutation, no I/O, no random). Pure functions are trivially testable (no mocking required), composable, memoizable, and safe to parallelize. The goal of FP is to maximize the percentage of your code that is pure functions.

1 of 4
🔍FP is a spectrum, not all-or-nothing
You do not need to write Haskell to benefit from functional ideas. Even small wins help: prefer map/filter/reduce over imperative loops, avoid mutating function arguments, extract pure utility functions, and name predicates. These incremental changes make code easier to test and reason about without a full paradigm shift.

🎯 Quick Check

What makes a function 'pure'?

Functional Programming in JavaScript: summary

Key points

  • Pure functions are easy to test and reason about.
  • Immutable updates ({ ...obj, key }, toSorted(), with()) avoid shared-state bugs.
  • Higher-order functions take or return functions: map, debounce, middleware.
  • Readability beats cleverness; a plain loop is fine when it's clearer.

Frequently asked questions

›What is a pure function?

A function that always returns the same result for the same arguments and doesn't change anything outside itself (no mutation, I/O or globals).