What is currying in JavaScript?
JavaScriptThe short answer
Currying transforms a function that takes multiple arguments into a series of functions that each take one argument. Instead of calling add(1, 2, 3), you call add(1)(2)(3). Each call returns a new function until all arguments are provided, then the final result is returned.
How it works
// Normal functionfunction add(a, b, c) { return a + b + c;}add(1, 2, 3); // 6// Curried versionfunction curriedAdd(a) { return function (b) { return function (c) { return a + b + c; }; };}curriedAdd(1)(2)(3); // 6Each function takes one argument and returns the next function in the chain.
With arrow functions
Arrow functions make currying much cleaner:
const add = (a) => (b) => (c) => a + b + c;add(1)(2)(3); // 6// You can also save intermediate functionsconst add1 = add(1);const add1and2 = add1(2);add1and2(3); // 6A generic curry function
function curry(fn) { return function curried(...args) { if (args.length >= fn.length) { return fn(...args); } return function (...moreArgs) { return curried(...args, ...moreArgs); }; };}const add = curry((a, b, c) => a + b + c);add(1, 2, 3); // 6 — all at onceadd(1)(2)(3); // 6 — one at a timeadd(1, 2)(3); // 6 — mixedadd(1)(2, 3); // 6 — mixedThis generic curry function lets you call the function any way you want — all arguments at once, one at a time, or any combination.
Practical use case
const formatCurrency = curry((symbol, decimals, amount) => { return `${symbol}${amount.toFixed(decimals)}`;});const formatUSD = formatCurrency('$')(2);const formatEUR = formatCurrency('€')(2);const formatJPY = formatCurrency('¥')(0);formatUSD(19.99); // "$19.99"formatEUR(19.99); // "€19.99"formatJPY(1999); // "¥1999"You create specialized formatting functions by partially applying the currency symbol and decimal places.
Interview Tip
Show the simple manual currying example first, then the arrow function version. If asked to implement a generic curry function, show the version that checks args.length >= fn.length. The practical example (currency formatting) shows you understand when currying is actually useful.
Why interviewers ask this
Currying is a popular interview topic because it tests understanding of closures, higher-order functions, and function composition. Interviewers want to see if you can implement it and explain when it is useful in real code.