JavaScript Internals (521 Qs)•Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)
What are the differences between pure and impure functions?
Some of the major differences between pure and impure function are as below,
| Pure function | Impure function |
|---|---|
| It has no side effects | It causes side effects |
| It is always return the same result | It returns different result on each call |
| Easy to read and debug | Difficult to read and debug because they are affected by external code |
Practical Code Example
javascript
// Pure Function (No side-effects, predictable):
const add = (x, y) => x + y;
// Impure Function (Modifies external variable / depends on external state):
let count = 0;
const increment = () => { count++; return count; }; // Side-effect mutation!