Skip to main content
Albero Academy

Functional Programming

Write cleaner code with pure functions, immutability, higher-order functions, composition and currying.

Chapter 13JavaScript16 min readBeginner friendly
  • #JavaScript
  • #Functional
  • #Immutability

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 vs impure
// ✓ Pure — no outside state, no side effects
const withTax = (price) => price * 1.18;
// ✗ Impure — reads/mutates outside state
let 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:

don't mutate — derive
const state = { count: 1, tools: ["SQL"] };
const next = { ...state, count: state.count + 1 }; // new object
const 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:

functions that make functions
const multiplyBy = (factor) => (n) => n * factor; // returns a function
const double = multiplyBy(2);
double(10); // 20
const once = (fn) => { // run a function at most once
let 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:

compose small pieces
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 helper
const 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:

currying
const add = (a) => (b) => a + b;
const add10 = add(10); // pre-fill the first argument
add10(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

  1. 01Pure functions depend only on inputs and have no side effects — easy to test and reason about.
  2. 02Favour immutability: derive new data with spread instead of mutating.
  3. 03Higher-order functions take or return functions — the basis of map/filter and your own utilities.
  4. 04Composition chains small functions; a pipe helper reads left-to-right.
  5. 05Currying pre-fills arguments to build reusable, configured functions.