Curriculum Series

What is currying in JavaScript?

What is currying in JavaScript?

What is currying in JavaScript?

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

JAVASCRIPT
1// Normal function
2function add(a, b, c) {
3 return a + b + c;
4}
5add(1, 2, 3); // 6
6
7// Curried version
8function 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:

JAVASCRIPT
1const add = (a) => (b) => (c) => a + b + c;
2
3add(1)(2)(3); // 6
4
5// You can also save intermediate functions
6const add1 = add(1);
7const add1and2 = add1(2);
8add1and2(3); // 6

A generic curry function

JAVASCRIPT
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}
11
12const add = curry((a, b, c) => a + b + c);
13
14add(1, 2, 3); // 6 — all at once
15add(1)(2)(3); // 6 — one at a time
16add(1, 2)(3); // 6 — mixed
17add(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

JAVASCRIPT
1const formatCurrency = curry((symbol, decimals, amount) => {
2 return `${symbol}${amount.toFixed(decimals)}`;
3});
4
5const formatUSD = formatCurrency('$')(2);
6const formatEUR = formatCurrency('€')(2);
7const formatJPY = formatCurrency('¥')(0);
8
9formatUSD(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.

Finished reading?

Mark this topic as solved to track your progress.