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!


This content originally appeared on Go Make Things and was authored by Go Make Things

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.

Last Chance! A new session of the Vanilla JS Academy starts on Monday. Join today and get 30% off registration.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2023-03-27T14:30:00+00:00) WTF is currying in JavaScript!?. Retrieved from https://www.scien.cx/2023/03/27/wtf-is-currying-in-javascript/

MLA
" » WTF is currying in JavaScript!?." Go Make Things | Sciencx - Monday March 27, 2023, https://www.scien.cx/2023/03/27/wtf-is-currying-in-javascript/
HARVARD
Go Make Things | Sciencx Monday March 27, 2023 » WTF is currying in JavaScript!?., viewed ,<https://www.scien.cx/2023/03/27/wtf-is-currying-in-javascript/>
VANCOUVER
Go Make Things | Sciencx - » WTF is currying in JavaScript!?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/27/wtf-is-currying-in-javascript/
CHICAGO
" » WTF is currying in JavaScript!?." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2023/03/27/wtf-is-currying-in-javascript/
IEEE
" » WTF is currying in JavaScript!?." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2023/03/27/wtf-is-currying-in-javascript/. [Accessed: ]
rf:citation
» WTF is currying in JavaScript!? | Go Make Things | Sciencx | https://www.scien.cx/2023/03/27/wtf-is-currying-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.