What is the concept of pure functions and side effects?
Answer
A pure function has two properties: deterministic (same inputs always produce the same output) and no side effects (does not modify any external state — no mutating arguments, no global variables, no I/O, no DOM manipulation). Example: const add = (a, b) => a + b is pure. function addToCart(item) { cart.push(item); } is impure (mutates external state). Side effects are necessary in real applications — making API calls, updating DOM, writing to databases — but should be isolated to specific parts of the codebase. Benefits of pure functions: testable (no mocking needed — just call with inputs, check output), composable, memoizable, parallelizable (no shared state), and predictable. Functional programming paradigm pushes side effects to the "edges" of the system (at the application boundary) and keeps core logic pure. React components should be pure — given the same props, render the same output. Redux reducers must be pure functions.