#12) What is Currying in JS❓

myk

Mayank Yadav

Posted on August 2, 2021

#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!!!


💖 💪 🙅 🚩
myk
Mayank Yadav

Posted on August 2, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related