Currying is a functional programming technique in JavaScript where a function is transformed into a sequence of functions, each taking a single argument. Trade-offs include readability vs reuse, so test partial application and edge cases.
What is currying in JavaScript?
Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.
The Core Idea
Currying transforms a function that takes multiple arguments into a chain of functions, each taking one argument at a time. It allows partial application — creating new functions by pre-filling some arguments.
// Normal function
function add(a, b, c) {
return a + b + c;
}
// Curried version
function curriedAdd(a) {
return function(b) {
return function(c) {
return a + b + c;
};
};
}
console.log(curriedAdd(2)(3)(4)); // 9
Concept | Explanation | Example |
|---|---|---|
Partial Application | You can call a curried function one step at a time and reuse intermediate results. |
|
Function Composition | Currying makes it easier to build reusable pipelines of small functions. |
|
Closures in Action | Each inner function 'remembers' the outer function’s arguments through closure. |
|
Modern Short Syntax
You can write curried functions using arrow functions for brevity:
const add = a => b => c => a + b + c;
console.log(add(1)(2)(3)); // 6
Practical scenario
You build an API client where you first fix the base URL, then inject auth, then pass endpoint params: api(baseUrl)(token)(path).
Common pitfalls
- Losing
thiscontext when curried functions call methods. - Over-currying for simple cases, which hurts readability.
- Confusing currying with default parameters or partial application.
Currying improves reuse but adds layers of functions. Unit-test each partial step to ensure the right value is captured.
Think of currying like a vending machine: instead of inserting all coins at once, you insert them one by one — and after the last coin, you finally get your snack.
- Currying transforms multi-argument functions into nested single-argument ones.
- Enables partial application and functional composition.
- Helps create reusable, modular logic in a clean, declarative way.
- Commonly used in frameworks like React, Redux, and functional libraries like Lodash or Ramda.