Clean β’ Professional
Understanding pure functions and side effects is essential for writing clean, predictable, and maintainable JavaScript code. They are a fundamental concept in functional programming.
A pure function is a function that:
Syntax Example
function add(a, b) {
return a + b; // Output depends only on inputs
}
console.log(add(2, 3)); // 5
console.log(add(2, 3)); // 5 (always same)
Another Example: Array Transformation
const numbers = [1, 2, 3];
function doubleArray(arr) {
return arr.map(num => num * 2); // Pure: doesn't modify original array
}
const doubled = doubleArray(numbers);
console.log(doubled); // [2, 4, 6]
console.log(numbers); // [1, 2, 3] (original unchanged)
A side effect occurs when a function modifies something outside its scope or interacts with external systems. Examples include:
let counter = 0;
function incrementCounter() {
counter += 1; // Side effect: modifies external variable
return counter;
}
console.log(incrementCounter()); // 1
console.log(incrementCounter()); // 2 (depends on external state)
| Aspect | Pure Function | Side Effect |
|---|---|---|
| Output | Deterministic (same input β same output) | May vary depending on external state |
| External Modification | None | Modifies external variables, objects, or systems |
| Predictability | Highly predictable | Less predictable |
| Testing | Easy to test | Harder to test |
map, filter, and reduceNot all side effects are bad. Some operations must interact with external systems (e.g., API calls, UI updates). The key is to isolate side effects from pure logic:
// Pure function for calculation
function calculateTotal(prices) {
return prices.reduce((sum, price) => sum + price, 0);
}
// Side effect separated
function displayTotal(total) {
console.log(`Total Price: ${total}`);
}
const total = calculateTotal([10, 20, 30]);
displayTotal(total); // 60Β