In this post, we will try to understand Currying in Javascript, how we can make curry function and the reason behind the currying concept.

executed, gives us the ability to create factory functions — functions that can add a specific value to their argument.

Hope this article was helpful. Subscribe and leave your comments to support.

### What is Currying in Javascript?

**Currying is the process of taking a function with multiple arguments and turning it into a sequence of functions each with a single argument and eventually resolve to a value.**
Not clear with the definition,right?

Lets understand with example, in which we have written a function to find the volume.

Lets understand with example, in which we have written a function to find the volume.

// volume function definition using arrow

const volume = (l, b, h) => (l * b * h);

// calling volume function

volume(3,2,4); // 24

Now here, when we want to call this volume function, we need to pass all the three arguments at one time. Consider a scenario where you get length, breadth and height at different part of the function or javascript file. How can we achieve this? To answer such scenario, we can use currying in javascript.

### Making curry function

Let rewrite the above volume function using currying.

const volume = (l) => {

return (b) => {

return (h) => l * b * h;

}

};

// calling this volume function

volume(3)(2)(4); // 24

Basically, when we call volume by passing first single argument(here 3),it return a function where we pass second single argument(here 2) which return another function where we pass third single argument (here 4) which returns the final result( here 24).

The word curry has been derived from the mathematician named

In algebra, we can compose a new function from two different function. e.g.

m: a -> b

n: b -> c

the two can be composed to make new function

p: a -> c by p = m.n i.e. m(n(x)) in algebra

Following this rule in javascript, we can derive

const n = a => a + 5;

const m = a => a * 2;

const p = x => m(n(x));

p(10); //=> 30

// calling this volume function

volume(3)(2)(4); // 24

Basically, when we call volume by passing first single argument(here 3),it return a function where we pass second single argument(here 2) which return another function where we pass third single argument (here 4) which returns the final result( here 24).

The word curry has been derived from the mathematician named

**'Haskell Curry'.**In algebra, we can compose a new function from two different function. e.g.

m: a -> b

n: b -> c

the two can be composed to make new function

p: a -> c by p = m.n i.e. m(n(x)) in algebra

Following this rule in javascript, we can derive

const n = a => a + 5;

const m = a => a * 2;

const p = x => m(n(x));

p(10); //=> 30

### Behind the scene

The root cause because of which we are able to make curry function is*in javascript.***closure****makes currying possible in JavaScript. It’s ability to retain the state of functions already**

__Closure__executed, gives us the ability to create factory functions — functions that can add a specific value to their argument.

Hope this article was helpful. Subscribe and leave your comments to support.

**Comment me if any query.**

**Thank you!!**

## Comments

## Post a comment