DEV Community

Cover image for Understanding JavaScript Currying with a Real-World Application

Understanding JavaScript Currying with a Real-World Application

Richa on January 20, 2025

Have you seen the word "currying" in JavaScript and wondered what it meant? In this blog, we'll look at the concept of currying, break it down with...
Collapse
 
pengeszikra profile image
Peter Vivo • Edited

You are totally right a curryng is a great tool of JS programming.
Great to put into the table!

But the standard function is a different question.
A way more elegant declaration of currying is a arrow function.

/** @type {(bun:string) => (patty:string) => (topping:string) => string} */
const makeBurgerCurried = 
   (bun) =>
   (patty) =>
   (topping) =>
      `Your burger has: ${bun} bun, ${patty} patty, and ${topping} topping.`
;
Enter fullscreen mode Exit fullscreen mode

I am using this technic in my program, there you can found the repo link in this post: dev.to/pengeszikra/javascript-grea...

Collapse
 
thedevricha profile image
Richa

Thanks for the comment!
You are correct, arrow functions are an easier way to write curried functions. They provide clarity and context, making them useful for this purpose.
I appreciate that you included a practical application of currying in your program. It's always interesting to see how theoretical concepts apply to practical implementations. I'll surely check that.

Collapse
 
the_riz profile image
Rich Winter

But this example (as is one of the above) isn't really currying, it is a predetermined order and is not reusable - It isn't returning a function that says "add the next argument" it is only returning a series of pre-determined arguments.
If I want to add, say, a layer that is "tomato" and another layer that is "onion" this function breaks.

Collapse
 
pengeszikra profile image
Peter Vivo

You totally right this example functions is don't really usefull.
Maybe this is much usefull example, to combine a different dependency
on the right moment.

const specificEventHandler = (state) => (event) => specificEventProcessing(event, state);

<button key={id} onClick={eventHandler(id)} />
Enter fullscreen mode Exit fullscreen mode
Collapse
 
documendous profile image
Documendous

Currying is usually seen as one of those advanced concepts. Breaking a function into smaller parts definitely improves reusability and clarity. I like it because it eliminates the need to repeatedly pass the same arguments and this makes your code more flexible and more maintainable. Good job!

Collapse
 
thedevricha profile image
Richa

Thanks for the comment!
Yeah, currying improves reusability and clarity by dividing down functions. It eliminates unnecessary parameters, increasing code flexibility and maintainability.

I'm glad you find it useful.

Collapse
 
vetrivendhan_b_a885118f74 profile image
Vetrivendhan B

I am happy to see many devs are shifting from OOP to Functional programming. Please stop using Class and this keywords in JavaScript. Thank me later.

Still many companies ask bind, call and apply uses in interviews. React stopped using Class components too.

Collapse
 
the_riz profile image
Rich Winter

There are many cases where a class based system as well as prototypal syntax make more sense, such as games for example. Functional Programming is great for operations on data, but can create code spaghetti chaos instead of organized properties when dealing with private getter/setters. Sometimes you just need encapsulation.

Collapse
 
juan_labrada_42e0d23118f4 profile image
Juan Labrada

Completly agree with you. Use the right tool for the right problem, OOP for modeling, FP for creating abstractions.

Thread Thread
 
thedevricha profile image
Richa

Nice points, everyone! Each approach works in its own context: OOP provides structure and encapsulation, while FP simplifies data operations and abstractions. The important thing is using the appropriate approach for the correct challenge.

Collapse
 
chris_lamb_7d72a6e849b5f7 profile image
Chris Lamb • Edited

The most common and useful case for currying is “configuring” a function that needs a specific signature.

So if you have to provide a call back to an onclick event of a button (which must be (e) => void), but need to pass it arguments first, then currying is brilliant for it.

const configureMyOnClick = (dependency) => (e) => {
If (dependency)
// do a thing.
}

Collapse
 
thedevricha profile image
Richa

Thank you for sharing the example! You are correct, currying is ideal for such situations. It enables functions to be modified to specific requirements, such as passing dependencies while keeping the necessary signature. A great real-world use case!

Collapse
 
_faisaldeshmukh_ profile image
Mohammad Faisal Deshmukh

This was really one of the best explanation of currying with simple examples... Really liked it!!

Collapse
 
thedevricha profile image
Richa

Thanks for appreciating
I’m glad you like it.

Collapse
 
arushkumar profile image
Arush Kumar

Great

Collapse
 
thedevricha profile image
Richa

Thanks for appreciating

Collapse
 
yatendra_tyagi_5c61cc13c5 profile image
Yatendra Tyagi

Mind-blowing

Collapse
 
thedevricha profile image
Richa

Thanks for appreciating

Collapse
 
quynhd3 profile image
quynhd

I likes this and went to googling more about it and I found... somewhere that marked Currying as Miscellaneous. What is your take on this?

Collapse
 
thedevricha profile image
Richa

Thank you for your comment!
Currying may be considered "miscellaneous" by some, but I believe it is a useful tool in functional programming. It benefits in applications such as partial application or developing reusable, flexible functions. While it may not be used every day, it is definitely worth learning!

Collapse
 
difficultworld24 profile image
Kartik Sharma

I love the simplicity of your writing... Awesome Content 👏🏻

Collapse
 
thedevricha profile image
Richa

Thanks for appreciating

Collapse
 
tamusjroyce profile image
tamusjroyce

Currying is garbage now that Javascript supports classes. You can do anything more manageable with a class. And there is a 1 to 1 syntax change for currying to classes

typescript to es3 converts class to function currying, for example

Just stop with this legacy syntax. Unless you really need to write es3 code