What is function currying in JavaScript
· Category: JavaScript
Short answer
Currying transforms a function that takes multiple arguments into a series of functions that each take a single argument, enabling partial application and composability.
How it works
A curried function returns a new function for each argument until all required arguments are supplied, then it returns the final result.
Example
const add = a => b => a + b;
const addFive = add(5);
console.log(addFive(3)); // 8
// General curry helper
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) return fn.apply(this, args);
return (...next) => curried(...args, ...next);
};
}
const multiply = (a, b, c) => a * b * c;
const curriedMultiply = curry(multiply);
console.log(curriedMultiply(2)(3)(4)); // 24
Why it matters
Currying improves reusability, supports partial application, and enables function composition in functional programming patterns. It helps create specialized functions from general ones without repeating arguments.