Currying In Javascript
(Benefits & Real-World Use Cases)
Currying is a technique in functional programming where a function that takes multiple arguments is transformed into a sequence of functions, each taking a single argument.
Simple Example
Let’s say you have a function that adds three numbers:
Get Meera Devi’s stories in your inbox
Join Medium for free to get updates from this writer.
Without Currying:
function add(a, b, c) {
return a + b + c;
}
console.log(add(1, 2, 3)); // 6With Currying:
function curriedAdd(a) {
return function(b) {
return function(c) {
return a + b + c;
};
};
}
console.log(curriedAdd(1)(2)(3)); // 6ES6 Arrow Function Version:
const curriedAdd = a => b => c => a + b + c;
console.log(curriedAdd(1)(2)(3)); // 6Real-World Use Cases
- Authorization Checks
const hasRole = role => user => user.roles.includes(role);
const isAdmin = hasRole('admin');
console.log(isAdmin({ roles: ['user', 'admin'] })); // true2. UI Event Handlers in React
const handleClick = id => event => {
console.log(`Clicked item ${id}`);
};
// Usage: <button onClick={handleClick(42)}>Click Me</button>3. Input Validators
const minLength = len => str => str.length >= len;
const isValidUsername = minLength(5);
console.log(isValidUsername("user1")); // trueHelps with Functional Programming
Currying works well with pure functions and higher-order functions — key principles in functional programming.
- Makes code more modular, composable, and reusable.
- Better testability
- Currying breaking down a function of multiple arguments into a series of unary (single argument) functions.
- Used often in functional programming and React with hooks and event handling.





