Pure function
A pure function is a function that always returns the same result for the same input and has no side effects. That is, it does not change external variables, the DOM, files, the network or anything at all outside itself.
Theory
TL;DR
- A pure function is deterministic: the same input always gives the same output.
- A pure function has no side effects: it changes nothing outside itself.
- It does not depend on external state, which makes it predictable and testable.
- What makes a function impure: changing external variables, the DOM, files, the network,
console.log(). Math.random()andDate.now()are impure too, because the result differs each time.- Pure functions are easy to cache (memoization) and safe to run in parallel.
- This is a fundamental concept of functional programming.
Quick example
// Pure function
function add(a, b) {
return a + b;
}
// Impure function
let counter = 0;
function increase() {
counter++; // side effect: changes an external variable
return counter;
}The two key properties
A pure function is a fundamental concept of functional programming. It is predictable, testable and independent of external state.
- Determinism. The same input always produces the same output.
- No side effects. The function changes nothing beyond its own boundaries.
Both properties are required. A function that returns a stable result but writes something to a log or a global object along the way is already impure.
An example of a pure function
function add(a, b) {
return a + b;
}It always returns the same result and changes nothing in the outside world. You can call it as many times as you like in any order and the behaviour of the program will not change because of it.
An example of an impure function
let counter = 0;
function increase() {
counter++; // side effect: changes an external variable
return counter;
}This function is impure, because its result depends on external state (counter) and it modifies that state. Two identical calls to increase() return different values, which already breaks determinism.
Other examples of impure functions
- reading from or writing to a file or a database;
- changing the DOM;
- calling
alert()orconsole.log(); - using random numbers (
Math.random()); - using the current time (
Date.now()).
A special case is mutating the arguments: if a function changes an object or array passed into it, it is impure too, even when it never touches a global variable.
// Impure: mutates the input array
function addItemImpure(list, item) {
list.push(item);
return list;
}
// Pure: returns a new copy
function addItem(list, item) {
return [...list, item];
}What pure functions are for
- Easy to test, because the result depends only on the arguments.
- Easy to cache (memoization).
- Safe for parallel execution.
- They increase the predictability and readability of code.
| Property | Pure function |
|---|---|
| Returns the same result | Yes |
| Depends on external variables | No |
| Changes external state | No |
| Example | Math.max(1, 5) |
| Opposite | A "dirty" function with side effects |
A pure function is a function that does not affect the outside world and always behaves the same way for the same input.
Common mistakes
- Calling a function pure when it mutates an argument.
list.push(item)changes the caller's object, so that is a side effect. - Forgetting about
console.log(). Formally it is output to the outside, so strictly speaking a function that logs is already impure. - Confusing purity with not having a
return. Purity is about determinism and side effects, not about whether there is a result. - Using
Date.now()orMath.random()inside. Pass such values in as an argument, and the function becomes pure and testable again. - Trying to make the whole program pure. Side effects are what make it do anything at all; you push them to the edges of the application and keep the core logic pure.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.