DEV Community

Cover image for #12) What is Currying in JS❓
Mayank Yadav
Mayank Yadav

Posted on

#12) What is Currying in JS❓

🔰Currying

✅It is a advanced technique of working with functions.
✅It is used in other languages also.
✅It is basically to transform a function with n arguments, to n functions of one or less arguments.

f(a, b, c) into f(a)(b)(c)
Enter fullscreen mode Exit fullscreen mode

💠For better understanding, here's an example👇
✅First create a helper function curryFun(n) that performs curring for two arguments.
✅curryFun(n) does the curring transformation.
image

💠As you can see, the implementation is straightforward
✅It has just two wrappers.

✅The result of curryFun(n) is a wrapper function(x).
✅When it is called like multiply(10), the argument is saved in the Lexical Environment, and a new wrapper is returned function(y).

✅Then this wrapper is called with (2) as an argument, and it passes the call to the original sum.

⚠So if anyone want to know about more advance currying implementation such as _.curry, just comment it, I'll help you out or anyone can also do!!!


Discussion (2)

Collapse
mr_eking profile image
Eric King

Quick FYI: The proper term is "currying", not "curring".

Collapse
myk profile image
Mayank Yadav Author

Type Error 😅.....my bad.
Thanks for correction.