What is currying in JavaScript?
The 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
1// Normal function2function add(a, b, c) {3 return a + b + c;4}5add(1, 2, 3); // 667// Curried version8function curriedAdd(a) {9 return function (b) {10 return function (c) {11 return a + b + c;12 };13 };14}15curriedAdd(1)(2)(3); // 6
Each function takes one argument and returns the next function in the chain.
With arrow functions
Arrow functions make currying much cleaner:
1const add = (a) => (b) => (c) => a + b + c;23add(1)(2)(3); // 645// You can also save intermediate functions6const add1 = add(1);7const add1and2 = add1(2);8add1and2(3); // 6
A generic curry function
1function curry(fn) {2 return function curried(...args) {3 if (args.length >= fn.length) {4 return fn(...args);5 }6 return function (...moreArgs) {7 return curried(...args, ...moreArgs);8 };9 };10}1112const add = curry((a, b, c) => a + b + c);1314add(1, 2, 3); // 6 — all at once15add(1)(2)(3); // 6 — one at a time16add(1, 2)(3); // 6 — mixed17add(1)(2, 3); // 6 — mixed
This 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
1const formatCurrency = curry((symbol, decimals, amount) => {2 return `${symbol}${amount.toFixed(decimals)}`;3});45const formatUSD = formatCurrency('$')(2);6const formatEUR = formatCurrency('€')(2);7const formatJPY = formatCurrency('¥')(0);89formatUSD(19.99); // "$19.99"10formatEUR(19.99); // "€19.99"11formatJPY(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.