JSworkingevergreen

Functional JavaScript

Data in, data out.

OOP has its place, but functional patterns β€” pure functions, immutability, composition β€” produce code that's easier to test and easier to reason about.

Pure functions, the contract

A pure function depends only on its arguments and produces no observable effect beyond its return value. The reward: it's trivially testable and freely cacheable.

The big four iterators

map transforms each item, filter keeps a subset, reduce collapses to a single value, flatMap does map-then-flatten in one pass. Most loop logic compresses into a chain of these.

Composition over inheritance

Build behaviour by combining small functions. compose(f, g, h) and pipe(f, g, h) (left-to-right) let you express "do this, then that" as data flow rather than control flow.

Immutability without the library

Spread ({...obj, key: val}) and array methods that return new arrays (map, filter, toSorted, toReversed) cover most cases.

Try it 3 examples

Pure functions and the big four

JSintro
Press Run to execute

Each of the big four collapses a loop into one expression: map squares every element to [0,1,1,4,9,25,64,169], filter keeps the even ones ([0,2,8]), reduce folds the array down to its sum 33, and flatMap maps-then-flattens nested arrays into [1,2,3,4] β€” and because square is pure, none of these mutate fib.