Currying in JavaScript:
A Functional Approach to Functions

Published on: April 1, 2025

What is Currying?

Currying is a functional programming technique that allows you to transform a function with multiple arguments into a sequence of functions that each take a single argument.

js
function add(a, b, c) {
	return a + b + c;
}

function curriedAdd(a) {
	return function (b) {
		return function (c) {
			return a + b + c;
		};
	};
}

add(1, 2, 3); // 6
curriedAdd(1)(2)(3); // 6

You need to understand closures and higher-order functions to understand currying. You can learn more about closures in the following article:

Understanding Closures: Capturing Lexical Environments

Why to Curry?

Inproves code reusability, and readability

js
function curriedAdd(a) {
	return function (b) {
		return function (c) {
			return a + b + c;
		};
	};
}

const baseValue = curriedAdd(100);
const baseValueWithGST = baseValue(10);
const baseValueWithGSTAndServiceTax = baseValueWithGST(5);

console.log(baseValueWithGSTAndServiceTax); // 115

We can reuse baseValue, baseValueWithGst and baseValueWithGstAndServiceTax in various parts of our code.

Lazy Evaluation

Currying allows for lazy evaluation. This means that a function does not evaluate, until all its arguments are received.

Dynamic currying

You can create a curried function that can take a variable number of arguments. This is called dynamic currying.

js
const add = args => args.reduce((a, b) => a + b, 0);

const addItems = (...args) => {
	return (...newArgs) => {
		if (newArgs.length === 0) return add(args);
		return addItems(...args, ...newArgs);
	};
};

const base = addItems(100);
const discount = base(-10);
const tips = discount(5);
const tax = tips(8);
tax(); // 103

Here, addItems is a higher-order function that returns a curried version of the original function.

js
// higher order function takes in a variable number of arguments
const addItems = (...args) => {
	// returns a function that also takes in a variable number of arguments
	return (...newArgs) => {
		if (newArgs.length === 0) return add(args);
		return addItems(...args, ...newArgs);
	};
};

The inner function returns the higher-order function again after merging the higher-order function arguments with the inner function arguments.

js
// for example, base = addItems(100), discount = base(-10)
// args = [100], newArgs = [-10]
return addItems(...args, ...newArgs);

Finally when we call addItems without any arguments, we execute the original function, with all the arguments.

js
// for example, tax()
// args = [100, -10, 5, 8], newArgs = []
if (newArgs.length === 0) return add(args);

Partial Application

Fancy word, simple concept. Partial application is when you fix a few arguments of a function and generate a new function that takes the remaining arguments in a curried fashion.

js
const largePizza = 720;
const sparklingWater = 120;
const salad = 200;
const tax = 160;

//                    partial application                curry
const base = addItems(largePizza, sparklingWater, salad)(tax);
base(); // 1200

Conclusion

Currying is a powerful technique that can help you create functions that are more reusable, readable, and flexible. It can also help you write more concise and expressive code.