Currying is a technique of transforming a function with multiple parameters into a chain of functions with one parameter each. Each function in the chain takes one argument and returns the next function, until the final result is returned.
// Regular function
function add(a, b, c) {
return a + b + c;
}
// Curried function
function curryAdd(a) {
return function(b) {
return function(c) {
return a + b + c;
};
};
}
// Usage
console.log(add(1, 2, 3)); // 6
console.log(curryAdd(1)(2)(3)); // 6
console.log(curryAdd(1)(2)); // function (expects third argument)Currying is like turning one big task into a chain of small tasks, where each next task depends on the previous one. Imagine assembling a LEGO constructor — first one piece, then another, and so on! 🧱
Currying is transforming a function so that instead of one call with all arguments, we can call it step by step, passing one argument at a time.
// Regular addition function
function sum(a, b) {
return a + b;
}
// Curried version
function currySum(a) {
return function(b) {
return a + b;
};
}
// Usage
console.log(sum(2, 3)); // 5
console.log(currySum(2)(3)); // 5// Regular function
function multiply(a, b, c) {
return a * b * c;
}
// Curried version
function curryMultiply(a) {
return function(b) {
return function(c) {
return a * b * c;
};
};
}
// Usage
console.log(multiply(2, 3, 4)); // 24
console.log(curryMultiply(2)(3)(4)); // 24function add(a) {
return function(b) {
return a + b;
};
}
const add5 = add(5); // Returns function expecting b
const result = add5(3); // 8 (5 + 3)
// Or directly:
const result2 = add(5)(3); // 8function multiply(a) {
return function(b) {
return a * b;
};
}
// Create specialized functions
const double = multiply(2); // Double a number
const triple = multiply(3); // Triple a number
console.log(double(5)); // 10
console.log(triple(5)); // 15// Curried function for range checking
function inRange(min) {
return function(max) {
return function(value) {
return value >= min && value <= max;
};
};
}
// Create specialized checks
const isChildAge = inRange(0)(12); // For children 0-12 years
const isAdultAge = inRange(18)(100); // For adults 18-100 years
console.log(isChildAge(8)); // true
console.log(isAdultAge(25)); // true// Curried function for filtering arrays
function filterBy(property) {
return function(value) {
return function(array) {
return array.filter(item => item[property] === value);
};
};
}
const users = [
{ name: 'John', age: 25 },
{ name: 'Peter', age: 30 },
{ name: 'Mary', age: 25 }
];
// Create filter for age 25
const filterByAge25 = filterBy('age')(25);
const result = filterByAge25(users);
// [{ name: 'John', age: 25 }, { name: 'Mary', age: 25 }]function add(a) {
return function(b) {
return a + b;
};
}
// ❌ Incorrect usage
// console.log(add(5)); // [Function] (not a number!)
// ✅ Correct usage
console.log(add(5)(3)); // 8function multiply(a) {
return function(b) {
return a * b;
};
}
// ❌ Error
// const result = multiply(5, 3); // undefined
// ✅ Correct
const result = multiply(5)(3); // 15Currying is a powerful technique that helps write more flexible and reusable code. Especially useful in functional programming! 💪
Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪