Side Effects Defined
What you'll learn: You'll learn exactly what qualifies as a "side effect" in functional programming—any observable change beyond returning a value.
What Is a Side Effect?
A side effect occurs when a function does something other than just compute and return a value. If your function reaches outside its own scope to change the world or interact with external systems, it's producing a side effect.
Think of a pure mathematical function like a vending machine that's been unplugged from electricity and the internet—you put in coins (inputs), and it gives you a snack (output) based purely on mechanical logic. A function with side effects is like a connected vending machine that also updates inventory databases, sends purchase notifications, and adjusts its pricing based on demand.
The Four Main Categories of Side Effects
1. I/O (Input/Output)
Reading from or writing to external sources: console logs, file operations, network requests, DOM manipulation.
2. Mutation
Modifying existing data structures, arrays, or objects that exist outside the function's scope.
3. Randomness
Calling Math.random() or Date.now()—functions that return different values each time, even with identical conditions.
4. Global State Changes
Modifying global variables, changing module-level state, or altering shared references.
Why It Matters
Identifying side effects helps you understand which functions are predictable and testable (pure) versus which ones have dependencies on the outside world. Pure functions are easier to reason about, debug, and test because they're self-contained.
Key Takeaway: Side effects are any observable changes beyond computing a return value—including I/O operations, mutations, randomness, and global state modifications. Recognizing them is the first step to writing more predictable code.