Same pattern: function sum(a) { return function(b) { if (b !== undefined) return sum(a + b); return a; }; }. Or use toString: function sum(n) { const fn = m => sum(n + m); fn.toString = () => n; return fn; }.
How do you implement sum(1)(2)(3)(4)..(n) (Amazon question)?
More FAQs in Currying Interview Questions in JavaScript
function sum(a) { return function(b) { if (b !== undefined) return sum(a + b); return a; }; }. Call with no argument to terminate and return the accumulated sum.
function curry(fn) { return function curried(...args) { if (args.length >= fn.length) return fn(...args); return (...next) => curried(...args, ...next); }; }.
Currying transforms f(a, b, c) into f(a)(b)(c) - one argument at a time. Partial application presets some arguments and returns a function for the rest: f'(b, c) with a preset.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.