What is Currying in JavaScript?
Learn what currying is in JavaScript, how closures enable it, partial application, a manual curry implementation, and functional programming use cases.
Expected Interview Answer
Currying is a functional programming technique that transforms a function taking multiple arguments into a sequence of functions, each taking one argument, until all arguments have been supplied and the final result is computed. Instead of calling add(2, 3), a curried version is called as add(2)(3).
A curried function returns a new function for each argument received, closing over the arguments accumulated so far, until enough arguments exist to invoke the original logic. This relies entirely on closures to remember each partial argument between calls, since every intermediate returned function still has access to the outer parameters via the scope chain. Currying enables partial application, where you fix some arguments early to create specialized, reusable functions, such as creating a multiplyByTen function from a generic multiply function. Libraries like Lodash provide a generic curry() helper that automatically curries any function of a known arity, and currying is common in functional-style codebases for composing small, single-purpose functions together.
- Enables partial application to create specialized functions
- Improves function reusability and composability
- Makes code more declarative in functional programming styles
- Plays well with point-free style and function composition
- Common pattern in libraries like Redux and Lodash
AI Mentor Explanation
Currying is like a bowler who doesn't deliver the ball until three separate decisions are locked in one at a time: the grip, then the line, then the length, each choice returning a fresh readiness state waiting for the next. Only once all three decisions are supplied does the actual delivery finally happen.
Curried function chain: one argument per call until execution
add(2)
- Returns a new function
- Closes over the value 2
Returned function(3)
- Receives the second argument
- Has access to 2 via closure
Final computation
- 2 + 3 is computed
- Returns 5
Step-by-Step Explanation
Step 1
Take the first argument
Calling the curried function with one argument returns a new function instead of computing a result.
Step 2
Close over accumulated arguments
Each returned function retains access to previously supplied arguments through the closure and scope chain.
Step 3
Continue accepting arguments
Each subsequent call supplies one more argument and returns yet another function until enough have been collected.
Step 4
Execute once fully applied
Once the expected number of arguments has been supplied, the original logic finally runs and returns the result.
Step 5
Reuse via partial application
Stopping partway through the chain creates a specialized, reusable function with some arguments already fixed.
What Interviewer Expects
- Can implement a basic curry function manually
- Explains that closures are what retain accumulated arguments
- Distinguishes currying from simple partial application
- Gives a practical use case like creating specialized functions
- Knows libraries like Lodash provide a generic curry helper
Common Mistakes
- Confusing currying with just having multiple function parameters
- Believing currying only works for exactly two arguments
- Not knowing closures are the underlying mechanism enabling currying
- Overusing currying where it hurts readability without a clear benefit
- Mixing up currying and throttling/debouncing, unrelated concepts
Best Answer (HR Friendly)
“Currying is a way of breaking a function that needs several inputs into a chain of smaller functions that each take just one input at a time. It's mainly useful for creating reusable, specialized versions of a function by locking in some of the inputs ahead of time.”
Code Example
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(this, args);
}
return (...moreArgs) => curried.apply(this, [...args, ...moreArgs]);
};
}
function add(a, b, c) {
return a + b + c;
}
const curriedAdd = curry(add);
console.log(curriedAdd(1)(2)(3)); // 6
console.log(curriedAdd(1, 2)(3)); // 6
console.log(curriedAdd(1)(2, 3)); // 6Follow-up Questions
- How does currying rely on closures under the hood?
- What is the difference between currying and partial application?
- How would you write a generic curry() helper that works for any function?
- What are practical use cases for currying in real-world code?
- How does currying relate to function composition in functional programming?
MCQ Practice
1. What does a curried function return when called with fewer arguments than expected?
A curried function returns a new function that closes over the arguments received so far, waiting for the rest.
2. What JavaScript feature makes currying possible?
Closures let each returned function remember previously supplied arguments via the scope chain until enough are collected.
3. What does curriedAdd(1)(2)(3) evaluate to for add(a, b, c) { return a + b + c }?
Once all three arguments are supplied across the chained calls, the curried function finally invokes the original logic and returns 6.
Flash Cards
Define currying. — Transforming a function that takes multiple arguments into a chain of functions, each taking one argument at a time.
What mechanism enables currying? — Closures — each returned function retains access to previously supplied arguments via the scope chain.
How does currying relate to partial application? — Stopping partway through a curried chain creates a specialized, reusable function with some arguments pre-fixed.
Give an example curried call. — add(2)(3) instead of add(2, 3), where add(2) returns a function waiting for the second argument.