Currying in JavaScript

No, this has nothing to do with those delicious Indian curry dishes you might be thinking about, but with a functional technique used in JavaScript.

It seems confusing at first, but currying is simple. If you've never heard of it before, it's because this concept isn't native to JS.

Basically, it allows you to transform a function like ```f(a, b, c)``` into something like ```f(a)(b)(c)```. What that means is that you can split up a function with multiple arguments into a sequence of functions with one argument each.

Let's take a basic example.

```js
const newUser = function(name, age, skill) {
  return {
    name,
    age,
    skill
  }
}
	
newUser("John", 27, "JS")
```

Now to the curry part:

```js
const newUser = function(name) {
  return function(age) {
    return function(skill) {
      return {
        name,
        age,
        skill
      }
    }
  }
}
	
newUser("John")(27)("JS")
```

Add in some arrow functions and voila:

```js
const newUser = 
  name => 
    age => 
      skill =>
      {
        name,
        age,
        skill
      }
```

The purpose of all this you might ask?

Think about situations when you don't have the complete data available in the beginning and you still need your function to gradually pass through your app and receive its arguments step by step as more and more data is arriving, until you add the final argument and receive the output.

vladcraciun
31 Aug 2020