Pure Functions in JavaScript (original) (raw)
Last Updated : 17 Dec, 2024
A Pure Function is a function (a block of code) that **always returns the same result if the same arguments are passed.
- Pure functions return consistent results for identical inputs.
- They do not modify external states or depend on mutable data.
- Often used with immutable data structures to ensure reliability.
- Their independence from external states makes them highly reusable. JavaScript `
function add(a, b) { return a + b; } console.log(add(2, 3)); console.log(add(2, 3));
`
- It always returns the same result for the same input.
- It does not modify any external variables or state.
**Note: Generally, we use the Pure function in JavaScript for any purpose.
Characteristics of Pure Functions
- **Deterministic Output: For a given set of inputs, the output is always the same.
- **No Side Effects: The function does not:
- Modify variables outside its scope.
- Interact with external systems like APIs, databases, or DOM manipulation.
- Change mutable data.
- **Immutability: Pure functions do not change the input values; instead, they return a new value or object.
Example of a Function with Side Effects
Here, increment is not a pure function because it modifies the external variable count.
JavaScript `
let c = 0;
function inc() { c++; return c; } console.log(inc()); console.log(inc());
`
Impure Functions: What to Avoid
Impure functions produce unpredictable results or affect external states, which can lead to bugs and make your code harder to test.
JavaScript ``
let user = { name: "Meeta", age: 25 };
function updated(newAge) { user.age = newAge; return user; }
console.log(updated(26));
// Alters the global user
object
console.log(user.age);
``
Output
{ name: 'Meeta', age: 26 } 26
Real-World Applications of Pure Functions
- **Data Transformation: Use pure functions in map, filter, and reduce operations for processing data.
- **State Management: Libraries like Redux emphasize pure functions for state updates.
- **Unit Testing: Pure functions are ideal for unit tests because they have predictable outputs.
- **Performance Optimization: Pure functions are easily memoized, as their outputs depend solely on inputs.