DEV Community

JavaScript: Currying

Ravina Deogadkar on July 24, 2022

Hi everyone! After a long gap I was refreshing my JavaScript understanding and went through many blogs and videos. JavaScript is like a magic you w...
Collapse
 
frankwisniewski profile image
Info Comment hidden by post author - thread only accessible via permalink
Frank Wisniewski

I wonder about the point of copying code from "Javascript.info" and posting it here.
Wouldn't it be enough to post the link...

Collapse
 
deogadkarravina profile image
Ravina Deogadkar

Thanks for suggesting, I will add it in reference

Collapse
 
jonrandy profile image
Jon Randy 🎖️

What happened in the 'application' section? 🤔

Collapse
 
deogadkarravina profile image
Ravina Deogadkar

It's use cases where we can apply currying

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Really? OK, I'll bite... please give examples of using currying to:

  • Manipulate the DOM
  • Trigger an event listener
Thread Thread
 
deogadkarravina profile image
Ravina Deogadkar

You can check this codepen example for manipulating dom codepen.io/emilsone/pen/jOwNgde

and in react where you attach a event handler onclick event to work as
const handleClick = (value)=>{
return (e)=>{
setValue({[value]: e.currentTarget.value }
}
}

You can also use it to create a function which will accept a event and the element as argument and will be used to add event listener or remove event listener
function on(type, element){
element.addListener(type, ()=>alert("clicked"));
return (a)=>element.removeListener(type,(a)=>alert("element removed"+a));
}

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

Currying isn't manipulating the DOM, nor is it triggering an event listener. Currying is merely a process to convert a function that takes multiple arguments into a sequence of functions that each takes a single argument.

Thread Thread
 
deogadkarravina profile image
Ravina Deogadkar

Yes right currying is a process to convert a function with multiple arguments to multiple functions with single argument. Currying is used to manipulate the DOM and triggering an event listener. So this are "use-cases/scenarios" where you can use currying. That's the reason I have written them in "application" section and not in the "examples" section.

Thanks for reading my blog I will definitely share a blog to discuss currying use-cases/scenarios.

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

It seems we're fighting a language battle that is going round in circles

Thread Thread
 
deogadkarravina profile image
Ravina Deogadkar

Very well, I got what you want to say but I have used currying in the following scenario and I feel mentioning them so that reader get an idea where they can apply it.

Some comments have been hidden by the post's author - find out more