JavaScript: Currying
Tags: javascript, webdev, programming, codenewbie
Published: 2022-07-24 06:47:00

Hi everyone! After a long gap I was refreshing my JavaScript understanding and went through many blogs and videos. JavaScript is like a magic you will never be able to understand it as whole. JavaScript as a base language do support many front end and back end libraries and framework. JavaScript is no more just a scripting language.

Today I am going to discuss about one of the most asked interview topic. **Currying** 

<img src="https://media.giphy.com/media/xUA7aQD2BSeMh6bGTu/giphy.gif" width=100 />

Trust me until now I have given a lots of interviews and this was asked to me in almost all interviews.

But to start with currying we need pre requisite knowledge of closures. To know more about closures you can read my [blog on closures](https://dev.to/deogadkarravina/javascript-closures-1kp3) where I have demonstrated closures and it's usecase in more detail.

##Currying
Currying is based on the concept of closures where a function of type f(a,b,c) is converted to f(a)(b)(c). Currying simply means evaluating function with multiple arguments and decomposing it into sequence of multiple functions with single argument. In other term a function is called by passing a argument which returns a new function accepting an argument and that in turn returns a new function accepting an argument and so on.

**Example**
![Example with normal function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/svr596mmryjn85chkbj0.png)

This could be rewritten using currying as follows.

![Example with currying](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8x48jponqu88pacc87jk.png)

Here add function becomes outer function for a function accepting b has an argument which in turn becomes outer function for a function accepting c has an argument. In term add function becomes a closure to encapsulated function limiting its scope only to the enclosed function.

**Advanced example**

![Advance currying example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jw5po8xp4od06hpczx8f.png)

And the function can be used as

![Advance currying function use](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jtv3foq2gsm9iee8kzfg.png)

##Closure and Partial application

Closures are formed by nesting functions but at what level? Does it depends upon number of arguments passed to a function? What if it doesn't.

![Partial Application](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhhn74jvsaekp56l65rg.png)

Here only one function is nested although three arguments are passed and only one argument is decomposed.
No it doesn't form closure it forms partial application although they look similar but in closure the functions nested will depend on number of arguments passed to function. Partial application simply decompose a function into another function with smaller arguments.

##Application / Use-case

- Currying can be used to manipulate DOM in JavaScript.
- It is used to trigger event listener.
- In a functional programming currying is used to achieve higher order functions.

**Reference**

https://javascript.info/currying-partials

That's all for today until then Happy Coding!...

<img src= "https://media.giphy.com/media/1jkVi22T6iUrQJUNqk/giphy.gif" width=100 />