Skip to main content Accessibility Feedback

WTF is currying in JavaScript!?

Every now and then, someone asks me to write an article about currying in JavaScript.

Currying is when you create a function that returns a function. Any time I’ve tried to research it, though, I come across tons of articles with examples like this…

// Regular function
function addNumbers (a, b, c) {
	return a + b + c;
}

// Curried function
function addNumbersCurried (a) {
	return function (b) {
		return function (c) {
			return a + b + c;
		};
	};
}

addNumbers(1, 4, 3);
addNumberCurried(1)(4)(3);

And I say to myself, why the fuck would anyone want to do this!?!

Well, after ranting a bit about this the other day, my friend, D&D buddy, and fellow Comic Sans enthusiast Alex Riviere wrote an amazing article on currying with actual useful examples and when and why you might want to use it.

So “when” is the bigger thing as to why you want to write a curried function. Curried functions are great when you know want to provide a common value across a function call multiple times, but it may not be a static value.

So let’s make a practical example of a Currying Function. The example that you have likely run into before is the authenticated fetch handler…

Now anywhere in our code base that we want to fetch from our authorized API endpoint, we don’t have to pass a token around.

If you’ve ever wanted to know more about currying or found it as weird and confusing as I did, go read Alex’s entire post.

It’s got actual useful code examples in it that make it all click.