Deterministic Output: Same Inputs, Same Results
You'll learn: Why pure functions always produce identical outputs when given identical inputs, making them predictable and testable.
The Core Principle
A deterministic function behaves like a mathematical equation: plug in the same numbers, get the same answer every time. No surprises, no randomness, no hidden dependencies. This is the first hallmark of a pure function.
When you call add(2, 3), you expect 5. Not 5 sometimes and 6 other times. Not 5 on Mondays and 4.99 on Fridays. Always 5.
Why It Matters
Deterministic functions are:
- Predictable: You know exactly what you'll get
- Testable: Write a test once, it stays valid forever
- Debuggable: Same inputs always trigger the same code path
- Cacheable: Store results because they never change
The Analogy
Think of a vending machine that always gives you a Coke when you press B2. That's deterministic. Now imagine one that sometimes gives you a Coke, sometimes a Sprite, and occasionally nothing—based on hidden factors like the time of day or how many people used it before you. That's non-deterministic chaos.
What Breaks Determinism
Functions lose determinism when they depend on:
- Current date/time
- Random numbers
- External state (global variables, file contents, network data)
- User input during execution
// Deterministic ✓
function double(n) {
return n * 2;
}
// Non-deterministic ✗ (depends on current time)
function addTimestamp(n) {
return n + Date.now();
}
Key Takeaway: Pure functions are deterministic—identical inputs always produce identical outputs, making your code predictable, reliable, and easier to reason about.