What You'll Learn
You'll learn what currying is and how it transforms functions to accept arguments one at a time instead of all at once.
The Core Idea
Currying is a functional programming technique that converts a function taking multiple arguments into a sequence of functions, each taking a single argument. Instead of calling add(2, 3), currying lets you call add(2)(3).
Think of it like a vending machine that requires exact change: rather than accepting all coins at once, you feed them in one at a time until the machine has enough to dispense your item.
How It Works
A normal function might look like this:
function add(a, b, c) {
return a + b + c;
}
add(1, 2, 3); // 6
The curried version breaks this into nested functions:
function curriedAdd(a) {
return function(b) {
return function(c) {
return a + b + c;
};
};
}
curriedAdd(1)(2)(3); // 6
Each function waits for one argument, then returns another function expecting the next argument—until all arguments are collected and the final result is computed.
Why Curry?
Currying enables partial application: you can pre-fill some arguments and reuse the resulting function:
const addOne = curriedAdd(1);
const addOneAndTwo = addOne(2);
addOneAndTwo(3); // 6
addOneAndTwo(5); // 8
This promotes code reusability and helps create specialized functions from general ones—a key pattern in functional programming.
Key Takeaway: Currying transforms multi-argument functions into a chain of single-argument functions, enabling flexible partial application and function composition.