Currying in Javascript

Radheshyam Kumar
3 min readJul 1, 2022

--

In this article, we’re going to explore what is currying in Javascript,how to implement it and at last we will be solving some amazing problems on different variations of currying . So let’s get started..

What is currying in JavaScript?

So the currying is nothing but a technique to evaluate the function which is taking multiple arguments,into a sequence of functions which will take single argument. We have seen many times the function call like **sum(1,2,3)** but what about if we call this function as **sum(1)(2)(3)**, this _transformation of function_ is currying. Here if you observe closely , you will get that instead of taking all the arguments at once , function takes only first argument and return a function which takes second argument which again returns a function that takes third argument and so on untill all arguments get consumed.

Examples :

Suppose you have the length, breadth and height of cuboid and you want to erite a function which return volume of cuboid.The below function will console the result as we all know.

Without currying

With currying

Here in the example with currying, findVolume() function is taking only one argument (i.e. length) and from inside, it is returning an anonymous function, recieving breadth as parameter and which again returning a function height as parameter and lastly it is returning the result when all all the arguments get consumed. One thing here to note that inner function is getting access to all these variables or parameters because of closure. As we are returning a function from a function , so it will always hold access to the variable of their parent, doesn’t matter where we invoke them. Closure always contains the function definition along with the lexical environment of the parent, both things remain connected as a bundle.

shortcut using arrow function : we can write the above function in one line as arrow function.

Variations

Now we will see some vairities of questions on Currying

Variety 1 : sum(1)(2)(3)(4)()

In above example think of example like : sum(1)(2)(3)(4)() => sum(3)(3)(4)()=>sum(6)(4)()=> sum(10)() here at last when we call sum(10) it is returning a function with a parameter “b” but “b” will be undefined here bcz of () i.e, absence of arguments so only we have to return “a” that will be 10;

using one liner arrow function

Variety 2 : sum(1,2)(3,4)(5,6) & sum(1,2)(3,4)(5,6)(7,8)()

solution 1 : Same as single argument question we can solve this too only difference is that instead of one argument at a time function will be taking two argument.

What if sum(1,2)(3,4)(5,6)(7,8)() so if we follow above approach then no of line will keep increasing with number of argument .

Variety 3 : sum(1,2,3,…n1)(4,5,6,…n2)…(n3)() any number of arguments

Simple idea is to reduce e.g sum(1,2,3,4,5)(6,7)() to sum(1+2+3+4+5)(6+7)()=> sum(15)(13)() to get the sum of all the arguments and this we have already solved.

Hope it will help you a lot 👏👏👏

--

--