What is Currying in JavaScript?
Learn what currying is in JavaScript, how closures enable it, its difference from partial application, with clear code examples and interview questions.
Expected Interview Answer
Currying is a technique that transforms a function taking multiple arguments into a sequence of functions that each take a single argument, returning a new function until all arguments are supplied.
Instead of calling add(a, b, c), a curried version is called as add(a)(b)(c). Each call captures one argument in a closure and returns the next function, only computing the final result once every argument has arrived. This enables partial application, where you fix some arguments early and reuse the specialized function later, and it works naturally with function composition and point-free style.
- Enables partial application and reusable specialized functions
- Improves composition in functional pipelines
- Reduces repeated arguments across many calls
- Makes configuration-then-use patterns cleaner
- Leverages closures to remember earlier arguments
AI Mentor Explanation
A curried function is like setting a batting order one player at a time. You lock in the opener, then the number three, then the number four, and only when the full lineup is submitted does the innings actually begin. Each choice is captured before the next, and the match starts only once every slot is filled.
Step-by-Step Explanation
Step 1
Start with a multi-argument function
Take a function like add(a, b, c) that expects all its arguments at once.
Step 2
Return functions instead
Rewrite it so add(a) returns a function expecting b, which returns a function expecting c.
Step 3
Capture arguments in closures
Each returned function closes over the arguments already supplied so they are remembered.
Step 4
Compute on the final call
Only when the last argument arrives do you have everything needed to produce the result.
Step 5
Use partial application
Fix early arguments to create specialized reusable functions like add5 = add(5).
What Interviewer Expects
- A clear definition tying currying to single-argument function chains
- Understanding of closures as the enabling mechanism
- The distinction between currying and partial application
- A working code transformation from normal to curried form
- A practical use case such as composition or reusable configuration
Common Mistakes
- Confusing currying with simply calling a function with fewer arguments
- Forgetting that closures are what retain earlier arguments
- Saying currying evaluates immediately instead of returning functions
- Mixing up currying and partial application as identical concepts
- Writing a curried function that loses the this binding unintentionally
Best Answer (HR Friendly)
“Currying is a way of writing a function so you provide its inputs one at a time instead of all at once. Each time you give one input, you get back a smaller function waiting for the next, which makes it easy to reuse partly-filled versions.”
Code Example
function add(a) {
return function (b) {
return function (c) {
return a + b + c;
};
};
}
console.log(add(1)(2)(3)); // 6
// Partial application: fix the first argument
const add10 = add(10);
console.log(add10(5)(2)); // 17function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(this, args);
}
return (...next) => curried.apply(this, args.concat(next));
};
}
const sum = (a, b, c) => a + b + c;
const curriedSum = curry(sum);
console.log(curriedSum(1)(2)(3)); // 6
console.log(curriedSum(1, 2)(3)); // 6
console.log(curriedSum(1)(2, 3)); // 6Follow-up Questions
- How does currying differ from partial application?
- How would you implement a generic curry function that supports variadic calls?
- Why are closures essential to currying?
- How does currying help with function composition?
- What are the performance trade-offs of deeply curried functions?
MCQ Practice
1. What does currying transform a multi-argument function into?
Currying converts f(a, b, c) into a chain like f(a)(b)(c), where each function takes one argument and returns the next.
2. Which JavaScript feature makes currying possible by remembering earlier arguments?
Each returned inner function closes over the arguments already supplied, so closures are what retain them until the final call.
3. Given const add = a => b => a + b, what does add(3)(4) return?
add(3) returns b => 3 + b, and calling that with 4 evaluates 3 + 4, which is 7.
Flash Cards
Define currying in one sentence. — Transforming a function of many arguments into a chain of functions that each take a single argument.
What makes currying work under the hood? — Closures — each returned function remembers the arguments already provided.
Currying vs partial application? — Currying always yields unary functions; partial application fixes some arguments and may still take several at once.
When does a curried function compute its result? — Only after the final required argument is supplied, completing the chain.