tech

Functional Programming in JavaScript

javascriptfunctionalpatterns

You do not need a functional library to write functional JavaScript. The array methods are enough.

Core Trio

const result = data
  .filter(x => x.active)
  .map(x => x.name)
  .reduce((acc, name) => acc + ", " + name, "")
  • map transforms each element
  • filter selects elements
  • reduce accumulates to a single value

Avoid mutations. Prefer expressions over statements. Your code will be more predictable.