Pure Functions and Side Effects
Write predictable, testable functions.
What Makes a Function Pure
A pure function has two properties: given the same inputs it always returns the same output, and it causes no observable side effects. Pure functions are predictable and easy to test.
function add(a, b) {
return a + b;
}
console.log(add(2, 3));
console.log(add(2, 3)); // always 5Deterministic Output
Purity means determinism. The same arguments must always map to the same result, with no dependence on hidden state, time, or randomness.
function double(n) {
return n * 2;
}
console.log(double(10)); // always 20All lessons in this course
- Pure Functions and Side Effects
- Declarative Data Transformation
- Avoiding Mutation
- Point-Free Style