DEV Community

A practical example of how to use Currying in Javascript

Alessio Michelini on September 22, 2021

A lot of times I see explanations of concepts in X language that, while I get what they do, I think "where the hell I can use that?". Or some other...
Collapse
 
bobdotjs profile image
Bob Bass

Wow, I'm a full-time JavaScript developer and I am familiar with currying, if I'm being honest it's something I picked up when I was studying for interviews.

I recall seeing the double arrow function once and not making the connection that it was a curried function.

It's pretty wild to make that connection now.

Collapse
 
darkmavis1980 profile image
Alessio Michelini • Edited

Hi! Yes I know, that's what I would normally do, but I feel that with the limited width of dev.to pages, using the return makes it a bit more readable ;-)
But yeah, in my normal coding I would do as you suggested

Collapse
 
j836 profile image
Prajwal Jain

Thanks for this awesome blog.
I wanted to know why
Image description

**filtering** function does not **look up** in its parent scope where name is present.

Collapse
 
kjk93 profile image
Kyle Kravette

Thank you so much for putting this into a practical application! Much easier to read and understand.

Collapse
 
soham_somnathshet_457987 profile image
Soham Somnath Shet

Hey Alessio ,Nicley Explained
But a query🤔

`const filtering = item =>{console.log(name); item.name !== name?true:false;

const filterByName = (list, name) => {
return list.filter(filtering);
}`
name variable in above example is compared with empty string ? shouldnt be the undefined it must be comparing
i have logged its printing an empty string

Collapse
 
gokhanipek profile image
Gökhan İpek

Thanks for the article, when learning a new concept there are times when people think like 'okay it looks cool but where/when should I use it' currying is probably one of the top concepts when it comes to this.
I remember thinking of a useful usage case to add to my article that's explaining currying, months ago. I'll bookmark yours and show people when I talk about it next time!

Collapse
 
andrewpierno profile image
Andrew Pierno • Edited

This is a solid tutorial, Alessio!
Would you be interested in writing some tutorials for our companies? Happy to pay!
You can either DM me on twitter at twitter.com/AndrewPierno or fill out this little airtable form airtable.com/shrN6S3NMZ7oxRXTt.

Collapse
 
princylora profile image
PrincyLora

I was wondering the example which you shared is not practical too.

the first function itself seems to be concise and DRY. Why make it complicated using currying ? 🤔

const filterByName = (list, name) => {
return list.filter(item => item.name !== name);
}

console.log(filterByName(list, 'John'));

Collapse
 
itsraghz profile image
Raghavan alias Saravanan Muthu

Good one. Thank you for sharing.

Collapse
 
nicm42 profile image
Nic

Now I get why you'd use currying, thank you.

Collapse
 
marcelx8 profile image
Marcelx8

Great example, thanks!

Collapse
 
adityaksi profile image
Aditya Singh 🚀

Best explanation