Currying is a functional programming technique where a function with multiple arguments is transformed into a sequence of functions, each taking one argument at a time.
It allows partial application, meaning you can fix some arguments and reuse the function later.
1️⃣ Basic Example
Normal function:
function add(a, b, c) {
return a + b + c;
}
console.log(add(1, 2, 3)); // 6
Curried version:
function add(a) {
return function(b) {
return function(c) {
return a + b + c;
};
};
}
console.log(add(1)(2)(3)); // 6
✅ Each function takes one argument and returns a new function for the next argument.
2️⃣ Using Arrow Functions
const multiply = a => b => c => a * b * c;
console.log(multiply(2)(3)(4)); // 24
- Very concise with arrow functions
- Still supports partial application
const double = multiply(2); // b => c => 2 * b * c
console.log(double(3)(4)); // 24
3️⃣ Practical Use Case: Logging
const log = level => message => console.log(`[${level}] ${message}`);
const info = log("INFO");
const error = log("ERROR");
info("This is an info message"); // [INFO] This is an info message
error("This is an error message"); // [ERROR] This is an error message
- You can preset some arguments (
level) and reuse the function easily.
⚡ In short:
Currying transforms a multi-argument function into nested single-argument functions, enabling partial application and cleaner, reusable code.
It’s widely used in functional programming and libraries like Ramda or Lodash/fp.