JavaScript·Advanced·Coding·1 min read
How is currying implemented with closures?
Short interview answer
Each returned function closes over arguments received at an earlier stage. The final function combines all captured values to produce the result. A generic curry utility also tracks arity and accumulates arguments across calls.
Example
function curry(fn) { return function curried(...args) { return args.length >= fn.length ? fn(...args) : (...more) => curried(...args, ...more); // closes over `args` };}
const sum3 = curry((a, b, c) => a + b + c);sum3(1)(2)(3); // 6sum3(1, 2)(3); // 6Key takeaway
Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.