Functional programming is a style — not a separate language — that favours pure functions, immutable data and composing small pieces. It underpins React, Redux and the array methods you already use. You don't need to go all-in; adopting a few habits makes any codebase more predictable and testable.
Pure functions
A pure function depends only on its inputs and has no side effects — same input, same output, every time. Pure functions are trivial to test and reason about:
// ✓ Pure — no outside state, no side effectsconst withTax = (price) => price * 1.18;// ✗ Impure — reads/mutates outside statelet total = 0;const addToTotal = (price) => { total += price; }; // side effect
Immutability
Instead of mutating data, produce new data. This prevents a whole class of "who changed this?" bugs and is required by React state. Use spread and non-mutating array methods:
const state = { count: 1, tools: ["SQL"] };const next = { ...state, count: state.count + 1 }; // new objectconst moreTools = { ...state, tools: [...state.tools, "Python"] };// state is untouched
Higher-order functions
A higher-order function takes or returns a function. map, filter and decorators are examples — and you can write your own to capture patterns:
const multiplyBy = (factor) => (n) => n * factor; // returns a functionconst double = multiplyBy(2);double(10); // 20const once = (fn) => { // run a function at most oncelet done = false, result;return (...args) => {if (!done) { result = fn(...args); done = true; }return result;};};
Composition
Build complex behaviour by combining small functions — the output of one feeds the next:
const trim = (s) => s.trim();const lower = (s) => s.toLowerCase();const dashify = (s) => s.replaceAll(" ", "-");const slug = (s) => dashify(lower(trim(s)));slug(" Hello World "); // "hello-world"// A generic compose helperconst pipe = (...fns) => (x) => fns.reduce((acc, fn) => fn(acc), x);const slugify = pipe(trim, lower, dashify);
Currying
Currying turns a multi-argument function into a chain of single-argument functions, so you can pre-fill some arguments and reuse the result:
const add = (a) => (b) => a + b;const add10 = add(10); // pre-fill the first argumentadd10(5); // 15// Handy for configuring callbacks:const greetWith = (greeting) => (name) => `${greeting}, ${name}`;["Aanya", "Rohan"].map(greetWith("Hi")); // ["Hi, Aanya", "Hi, Rohan"]
Practice
Rewrite an impure function that mutates a running total into a pure reduce. Write a pipe that cleans a string (trim → lowercase → remove punctuation). Then curry a discount(rate)(price) and pre-fill a 10% rate.
Key takeaways
- 01Pure functions depend only on inputs and have no side effects — easy to test and reason about.
- 02Favour immutability: derive new data with spread instead of mutating.
- 03Higher-order functions take or return functions — the basis of map/filter and your own utilities.
- 04Composition chains small functions; a pipe helper reads left-to-right.
- 05Currying pre-fills arguments to build reusable, configured functions.
