Functional programming (FP) has become a hot topic in modern JavaScript and other languages. Whether you’re working with React, Node.js, or just writing cleaner code, FP concepts like pure functions, higher-order functions, and immutability are essential.
In this blog post, let’s explore these three core ideas step by step with examples.
1. Pure Functions
A pure function is a function that:
- Given the same input, always returns the same output
- Has no side effects (it doesn’t alter external state, modify global variables, or depend on things outside its scope)
Think of a pure function like a math function —
f(x) = x + 2will always return the same result for the same input.
Example of a pure function:
js
function add(a, b) {
return a + b;
}
console.log(add(2, 3)); // Always 5
console.log(add(2, 3)); // Still 5
Example of an impure function:
js
let counter = 0;
function increment() {
counter++; // modifies external state
return counter;
}
console.log(increment()); // 1
console.log(increment()); // 2 (different result for same call)
Why pure functions matter?
- Easier to test
- Predictable and reliable
- No hidden side effects
- Safer for parallel or asynchronous execution
2. Higher-Order Functions
A higher-order function (HOF) is a function that either:
- Accepts functions as arguments, OR
- Returns a function as its output
- This is what makes JavaScript so expressive and powerful.
Examples of higher-order functions you already use:
js
// HOF: .map takes a function as an argument
const numbers = [1, 2, 3];
const squared = numbers.map(n => n * n);
console.log(squared); // [1, 4, 9]
// HOF: a function returning another function
function multiplier(factor) {
return function (n) {
return n * factor;
};
}
const double = multiplier(2);
console.log(double(5)); // 10
Higher-order functions allow:
- Code reusability
- Function composition
- Declarative and concise programming
For example:
js
function greet(name) {
return `Hello, ${name}`;
}
function withExclamation(fn) {
return function (name) {
return fn(name) + "!";
};
}
const excitedGreet = withExclamation(greet);
console.log(excitedGreet("Alice")); // Hello, Alice!
3. Immutability
Immutability means not changing data directly. Instead of modifying an object or array, we create a new version with the desired changes.
In JavaScript, this often means using methods like map, filter, reduce, or spread syntax, instead of mutating methods like push, splice, or directly reassigning object properties.
Example of mutable approach:
js
let arr = [1, 2, 3];
arr.push(4); // directly modifies the array
console.log(arr); // [1, 2, 3, 4]
Immutable approach:
js
let arr = [1, 2, 3];
let newArr = [...arr, 4]; // create a new array
console.log(arr); // [1, 2, 3]
console.log(newArr); // [1, 2, 3, 4]
Immutable object update:
js
const person = { name: "Alice", age: 25 };
const updatedPerson = { ...person, age: 26 };
console.log(person); // { name: "Alice", age: 25 }
console.log(updatedPerson); // { name: "Alice", age: 26 }
Why immutability matters?
- Prevents unexpected bugs (no shared mutable state)
- Easier debugging and reasoning about code
- Works perfectly with functional programming patterns
- Plays a crucial role in React and Redux
Final Thoughts
- Pure functions: No side effects, same input → same output.
- Higher-order functions: Functions that take or return other functions, enabling cleaner abstractions.
- Immutability: Don’t mutate data — create new copies instead.
Check out theYouTubePlaylist for great JavaScript content for basic to advanced topics.
Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ...CodenCloud
Top comments (0)