A pure function is a small, predictable unit of work: give it the same input and it produces the same result without changing shared or global state. This matters in JavaScript, where code that reads or updates external state can become difficult to reason about as an application grows. section 1.2 section 1.1
Contrast an increment function that changes a counter outside itself with one that receives the counter as an argument. The first call depends on a value that may have changed elsewhere; the second makes its dependency visible and leaves the original value alone. section 1.2
var increment = counter => counter + 1;
This version computes the next value from an explicit argument. Calling it with the same counter value always yields the same result. section 1.2
Purity does not mean an entire program never interacts with a database, page, console, or event stream. Instead, separate those effects from the logic that transforms data. A student-display task, for example, can be split into finding a record, formatting it as CSV, and appending the result to the page; the pieces are then joined into one flow. section 1.2 section 1.3
Pure functions support practical design habits:
- Keep a function focused on one purpose, with inputs and outputs that are easy to inspect. section 1.3
- Build larger behavior by composing simple functions whose output and input types agree. section 1.3
- Test logic without worrying that a call changed global state or depends on hidden changes. section 1.4 section 6.6
Refactoring a stateful calculation
Instead of updating an array in a loop, map each number to its square and return the resulting values. The calculation becomes an expression over input data rather than a sequence of mutations. Similarly, compute an average by combining total, size, and divide functions, each with a clear job. section 1.2 section 1.2
Pure functions make program flow easier to follow because results can be understood from the values passed in. They are especially useful as the inner logic of a program, while narrow boundary functions handle necessary effects such as DOM updates, database access, logging, and events. section 1.2 section 6.6


















