What is a pure function?
Short answer
A pure function always returns the same output for the same input and produces no observable side effects (no mutating external state, no I/O), which makes it predictable, testable, and easy to reason about in isolation.
Advertisement
AModel answer
A pure function always returns the same output for the same input and produces no observable side effects (no mutating external state, no I/O), which makes it predictable, testable, and easy to reason about in isolation.
// Pure
function add(a, b) { return a + b; }
// Impure — depends on and mutates external state
let total = 0;
function addToTotal(n) { total += n; }Real-world example
Preferring pure calculation functions in a reducer or utility layer for predictable, easily-testable behavior.
Likely follow-ups
- Can a pure function still throw an error?
Related topics