Sikau

Advanced JavaScript · Day 26 · about 25 min

Pure functions and reducers

Predictable code: same input, same output.

Code you can trust

A pure function always returns the same output for the same input and changes nothing outside itself. Pure code is easy to test, safe to reuse, and the backbone of tools like React and Redux.

Pure vs impure

let total = 0;
function addImpure(n) {
  total += n;
  return total;
}

const add = (a, b) => a + b;

console.log(addImpure(5), addImpure(5));
console.log(add(5, 5), add(5, 5));

The rest of this lesson

  • Reducers: state changes as pure functionsexplain
  • Side effects at the edgesexplain
  • Is it pure?predict
  • Pure or impure?match
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 27 lessons of Advanced JavaScript