Pure vs Impure Functions in JavaScript Explained
In JavaScript, understanding the distinction between pure and impure functions is a fundamental concept of functional programming that leads to cleaner, more predictable code. This article breaks down the definitions of pure and impure functions, examines practical code examples of each, outlines their key differences, and explains why choosing pure functions can significantly improve your application’s reliability and testability.
What is a Pure Function?
A pure function is a function that satisfies two core rules: 1. Deterministic: It always produces the same output for the same input arguments. 2. No Side Effects: It does not modify any state or variable outside its own scope, nor does it rely on mutable external data.
Example of a Pure Function
function add(a, b) {
return a + b;
}Whenever you pass 2 and 3 to
add(2, 3), it will always return 5. It does
not modify external variables, make network requests, or alter the
DOM.
What is an Impure Function?
An impure function fails one or both of the rules of a pure function. It either produces different outputs for the same arguments or triggers observable side effects outside its local environment.
Common causes of impurity include: * Modifying a global variable or
parameter by reference. * Using Math.random(),
Date.now(), or other non-deterministic APIs. * Performing
I/O operations (e.g., logging to the console, making HTTP requests,
interacting with a database). * Manipulating the DOM.
Example of an Impure Function
let total = 0;
function addToTotal(value) {
total += value; // Side effect: modifies external state
return total;
}Calling addToTotal(5) the first time returns
5. Calling addToTotal(5) a second time returns
10. The output changes despite the input being identical,
and the function mutates an external variable.
Key Differences
| Feature | Pure Functions | Impure Functions |
|---|---|---|
| Determinism | Always returns the same output for identical inputs. | Output can vary with the same inputs. |
| Side Effects | None. | Yes (modifies outside state, I/O, etc.). |
| External Dependencies | Depends only on passed arguments. | May depend on external, mutable variables or state. |
| Testability | Very easy to test in isolation. | Harder to test; often requires mocking or specific test setups. |
| Caching/Memoization | Can be safely cached based on input. | Cannot be reliably cached. |
Why Pure Functions Matter
Using pure functions improves software quality in several ways:
- Predictability: Because pure functions do not alter hidden state, code is easier to reason about and debug.
- Testability: Unit tests for pure functions require no mocks, stubs, or complex state management—simply pass inputs and assert the output.
- Refactoring and Reusability: Pure functions are self-contained, allowing them to be moved or reused anywhere across a codebase without unexpected behavior.
- Memoization: Since the output is guaranteed for specific inputs, results can be cached to optimize performance for computationally heavy operations.