Pure functions are an important concept in JavaScript and functional programming. A pure function always produces the same output when given the same inputs and does not cause side effects, such as changing external variables or modifying shared data.
Pure functions make code easier to understand, test, debug, and reuse. They are especially useful when building applications that need predictable behavior.
In this tutorial, you will learn what pure functions are, how they work, the difference between pure and impure functions, and how to write pure functions with practical examples.
What Are Pure Functions in JavaScript?
A pure function follows two main rules:
- It returns the same output whenever it receives the same inputs.
- It does not produce side effects by modifying external state or performing other observable actions.
Example of a Pure Function
function add(a, b) {
return a + b;
}
console.log(add(10, 20));
console.log(add(10, 20));
Output:
30
30
The add() function always returns 30 when called with 10 and 20. It does not modify external variables or change data outside the function.
This makes add() a pure function.
What Is an Impure Function?
An impure function either produces different results for the same inputs because of external state or causes side effects.
Example of an Impure Function
let total = 0;
function addToTotal(number) {
total += number;
return total;
}
console.log(addToTotal(10));
console.log(addToTotal(10));
Output:
10
20
The function produces different outputs for the same argument because it changes the external variable total.
Therefore, addToTotal() is an impure function.
Difference Between Pure and Impure Functions
| Pure Functions | Impure Functions |
|---|---|
| Produce the same output for the same inputs | May depend on changing external state |
| Do not modify external state | May modify external state |
| Are easier to test | Can require more setup to test |
| Have predictable behavior | May depend on the program’s current state |
Pure Functions with Arrays
When working with arrays, pure functions should avoid modifying the original array.
Example of a Pure Function
function doubleNumbers(numbers) {
return numbers.map(number => number * 2);
}
const values = [1, 2, 3];
console.log(doubleNumbers(values));
console.log(values);
Output:
[2, 4, 6]
[1, 2, 3]
The function returns a new array containing doubled values while leaving the original array unchanged.
The map() method is useful for this pattern. Learn more in our JavaScript Higher-Order Functions tutorial.
Benefits of Pure Functions
Pure functions provide several advantages:
- Predictability: The same inputs produce the same outputs.
- Easy testing: You can test a function by providing inputs and checking its result.
- Code reuse: Pure functions can be reused in different parts of an application.
- Simpler debugging: Their behavior does not depend on hidden external state.
- Safer data handling: Functions that avoid mutation help prevent unexpected changes to shared data.
Frequently Asked Questions
What is a pure function in JavaScript?
A pure function returns the same output for the same inputs and does not cause side effects.
What is the difference between pure and impure functions?
Pure functions do not modify external state and produce predictable results. Impure functions may modify external data or depend on changing state.
Can a pure function use variables?
Yes. A pure function can use local variables as long as its behavior remains predictable and it does not cause side effects.
Are arrow functions always pure?
No. An arrow function is not automatically pure. Its behavior determines whether it is pure or impure.
Why are pure functions useful?
Pure functions make programs easier to test, debug, maintain, and reason about because their results depend only on their inputs.