Curriculum
Pure and Impure Functions in JavaScript are important concepts in functional programming that describe how functions behave with inputs and outputs. Understanding Pure and Impure Functions helps beginners write predictable, reusable, and maintainable JavaScript code used in modern web development, ReactJS, and scalable applications.
Functions are one of the most important parts of JavaScript programming.
Some functions:
Other functions:
These behaviors divide functions into:
Understanding the difference helps developers:
Pure and Impure Functions are heavily used in:
A Pure Function:
Pure Functions depend only on:
Example:
function add(a, b){
return a + b;
}
console.log(add(2, 3));
Output:
5
No matter how many times the function runs:
This is a Pure Function.
Pure Functions:
Example:
function square(number){
return number * number;
}
This function depends only on:
numberNo external data is modified.
A Side Effect happens when a function:
Pure Functions avoid side effects.
An Impure Function:
Example:
let total = 0;
function add(value){
total += value;
return total;
}
Output:
Different outputs over time
This function:
totalTherefore:
Impure Functions:
Example:
let tax = 10;
function calculate(price){
return price + tax;
}
If:
tax changesThe output changes too.
This makes the function impure.
Example:
function multiply(a, b){
return a * b;
}
console.log(multiply(4, 5));
Output:
20
The function:
This is a Pure Function.
Example:
let count = 0;
function increment(){
count++;
return count;
}
Output changes every time:
This creates side effects.
| Feature | Pure Function | Impure Function |
|---|---|---|
| Same Output | Yes | Not always |
| External Data Dependency | No | Yes |
| Side Effects | No | Yes |
| Predictability | High | Low |
Pure Functions are easier to maintain.
Benefits include:
Modern frameworks encourage Pure Functions.
Pure Functions are used in:
Large applications rely heavily on predictable functions.
Impure Functions are necessary for:
Not all applications can avoid impure behavior.
Example:
function doubleNumbers(numbers){
return numbers.map(number => number * 2);
}
console.log(doubleNumbers([1, 2, 3]));
Output:
[2, 4, 6]
The original array remains unchanged.
Example:
let numbers = [1, 2, 3];
function addNumber(){
numbers.push(4);
}
addNumber();
console.log(numbers);
Output:
[1, 2, 3, 4]
The function modifies external data.
This makes it impure.
Beginners often:
Example:
let name = "Rahul";
function greet(){
return "Hello " + name;
}
This function depends on:
nameTherefore:
Functional Programming encourages:
Modern JavaScript frameworks heavily use functional programming principles.
Benefits include:
Understanding function behavior improves software quality.
Best practices include:
Readable and predictable functions improve maintainability.
Understanding Pure and Impure Functions helps developers:
These concepts are essential in modern JavaScript development.
Pure and Impure Functions in JavaScript describe how functions behave with inputs, outputs, and external data. Pure Functions are predictable and side-effect free, while Impure Functions modify or depend on external data. Understanding these concepts helps developers write cleaner and more reliable JavaScript applications.
A Pure Function always returns the same output for the same input and has no side effects.
An Impure Function depends on or modifies external data.
Side Effects occur when functions modify external variables or systems.
Pure Functions improve predictability, testing, and maintainability.
They are used in ReactJS, Redux, functional programming, and scalable applications.
WhatsApp us