Skip to content
UniCircle
EasyConceptualFunctions

Pure vs impure functions

Question JS-FUN-004

What makes a function pure? Why do frameworks like React and Redux care about purity?

Show the explanation

Model answer. A function is pure when:

  1. Given the same inputs, it always returns the same output.
  2. It has no side effects: it does not change its arguments, global state, the DOM, files, or the network.
JavaScript
const withTax = (price, rate) => price * (1 + rate);          // pure

let cartTotal = 0;
const addToCart = price => { cartTotal += price; };           // impure: changes outside state
const now = () => Date.now();                                 // impure: different every call

Why it matters:

  • Predictable and testable: no setup or mocks needed.
  • Safe to cache (memoise), because the result depends only on inputs.
  • React may call a component render more than once; renders must be pure or you get duplicated effects.
  • Redux reducers must be pure so time-travel debugging and change detection (prev !== next) work.

Real programs need side effects; the goal is to keep them at the edges (event handlers, effects) and keep the core logic pure.

Original UniCircle question. Last reviewed 22 Oct 2026.