Back
JavaScriptMediumNew
What is a pure function and why does it matter?
Same inputs always produce same outputs; no side effects.
Ideal Answer
A pure function satisfies two rules:
- Referential transparency — given the same arguments, it always returns the same result.
- No side effects — it does not mutate external state, I/O, DOM, globals, or arguments.
JavaScript
// Pure
const add = (a, b) => a + b
// Impure — depends on external state
let tax = 0.1
const withTax = (price) => price * (1 + tax)
// Impure — mutates input
const pushItem = (arr, item) => { arr.push(item); return arr }
Why interviewers care
- Predictable — easier to test and reason about.
- Cacheable — enables memoization.
- Parallel-safe — no hidden shared state.
- Works well with HOFs —
map,filter,reduceassume non-mutating callbacks.
Pure functions pair naturally with immutability patterns (spread, map instead of in-place mutation). Real apps need side effects at the boundaries — the functional style pushes impure I/O to the edges and keeps core logic pure.