Sitemap

Currying in JavaScript 🤩

3 min readJan 6, 2023

--

Currying in JavaScript 🤩

The great conversation I had this evening got me thinking about and revisiting a concept I’ve toyed with before — currying. But this time, I’d like to explore it with you all!

Currying is an advanced technique for working with functions. It’s used not only in JavaScript but in other languages as well.

Currying is a process in functional programming in which we can transform a function with multiple arguments into the sequence of the Nesting function ( in the form of Closures chaining of closures).

In other words, when a function, instead of taking all arguments at one time, takes the first one and returns a new function that takes the second one and returns a new function that takes the third one, and so forth until all arguments have been fulfilled.

Currying is a transformation of functions that translates a function from callable as f(a, b, c) into callable as f(a)(b)(c).

currying just transforms the function not call it.

Get Prathameshbhalke’s stories inĀ yourĀ inbox

Join Medium for free to get updates fromĀ thisĀ writer.

Let’s see an example first, to better understand what we’re talking about, and then practical applications.

Press enter or click to view image in full size

Transformation of Function into Closures chaining format Below

Press enter or click to view image in full size

Why it’s useful?

  1. Currying helps us avoid passing the same variable again and again.
  2. It helps to create a higher-order function

Currying transforms a function with multiple arguments into a sequence/series of functions each taking a single argument.

Press enter or click to view image in full size

As we see, function with the full arguments. Let’s create a curried version of the function and see how we would call the same function (and get the same result) in a series of calls:

Press enter or click to view image in full size

We could separate this sum(1)(2)(3) to understand it better:

Press enter or click to view image in full size

Let’s get to know how it works:

We passed 1 to the sum function:

Press enter or click to view image in full size

It returns the function:

Press enter or click to view image in full size

Now, sum1 holds the above function definition which takes an argument b.

We called the sum1 function, passing in 2:

Press enter or click to view image in full size

The sum1 will return the third function:

Press enter or click to view image in full size

The returned function is now stored in sum2 variable.

sum2 will be:

Press enter or click to view image in full size

When sum2 is called with 3 as the parameter,

Press enter or click to view image in full size

it does the calculation with the previously passed in parameters: a = 1, b = 2 and returns 6.

Press enter or click to view image in full size

The last function only accepts c variable but will perform the operation with other variables whose enclosing function scope has long since returned. It works nonetheless because of Closure šŸ”„

Closure makes currying possible in JavaScript. I hope you have learned something new about currying!

Thanks for reading this article ā™„ļø

If you have any question, please feel free to ping me on

https://portfolio-prathamesh.netlify.app

--

--

Written by Prathameshbhalke

Frontend Engineer at HMX Media