Currying in JavaScript

Ayesha Malik
The Startup
Published in
2 min readJan 9, 2021

--

Currying is a process in functional programming in which we can transform a function with multiple arguments into a sequence of nesting functions. In other words, it restructures a function so it takes one argument then returns another function that takes the next argument, and so on.

We will learn two ways to curry our functions. The first one is by using the bind function and the second is by using the concept of closures.

we can use multiply method to create multiply by 2, multiply by 3 and so on. We can make multiple copy of multiply method by pre setting the value of x as on line no 6.The actual definition of multiplyByTwo method looks like..

Currying a function using closures:

Here is the basic example of currying a function using closures. The inner function have access to x even after it’s returning, this is something like pre setting the value of x and can be access to inner method.

Why it’s useful ?

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

--

--

Ayesha Malik
The Startup

I’m a full-time software engineer who loves sharing knowledge to help others become better developers.