DEV Community

Cover image for Currying in Javascript
Jazsmith24
Jazsmith24

Posted on • Updated on

Currying in Javascript

In functional programming, having the ability to pass and return functions, brought on concepts such as Higher-order functions, pure functions, and currying. In this article, we will see how currying works and how it will be useful in our work as software developers.

What is currying?

Currying is the process where a function takes multiple arguments one at a time. Currying translates a function from callable as f(a, b, c) into callable as f(a)(b)(c). A curried function returns a new function that expects the next argument inline.

Currying lets you have light and clean looking functions. It also allows you to compose the sequence of your functions. It ensures that the particular sequence of calls is enforced and followed. The final output is only returned when all the dependencies have been passed through. The arguments are kept "alive" with closures and all are used in execution when the final function in the currying chain is returned and executed.

How would you use currying in your code?

Currying works by closures. The closure created by the nested functions retains access to each of the arguments. So inner function has access to all arguments.

Alt Text

Let’s look at a simple add function. It accepts three operands as arguments and returns the sum of all three as the result.

Example

function add(a,b,c){
 return a + b + c;
}

You can call it with too few (with odd results), or too many (excess arguments get ignored).

add(1,2,3) --> 6 
add(1,2) --> NaN
add(1,2,3,4) --> 6 //Extra parameters will be ignored.

Example 2

let greeting = function (a) {
    return function (b) {
        return a + ' ' + b
    }
}


let hello = greeting('Hello')
let morning = greeting('Good morning')

hello('Austin') // returns Hello Austin
hello('Roy') // returns Hello Roy
morning('Austin') // returns Good morning Austin
morning('Roy') //returns Good Morning Roy

The two functions created from greeting (hello and morning) each return functions that process the provided inputs to generate a greeting statement. They also take an argument which is the name of the person to be greeted.

In the above case, the greeting is also used as a function factory with the two functions hello and morning generated from it.

The inner function may also be called invoked after the first call as follows:

greeting('Hello There')('General Kenobi')
//returns Hello There General Kenobi

Why it’s useful?

  • Currying helps you to avoid passing the same variable again and again.
  • It helps to create a higher-order function. It is extremely helpful in event handling.
  • Little pieces can be configured and reused with ease.

Sources
https://javascript.info/currying-partials
https://blog.bitsrc.io/understanding-currying-in-javascript-ceb2188c339
https://medium.com/@kbrainwave/currying-in-javascript-ce6da2d324fe
https://codeburst.io/currying-in-javascript-ba51eb9778dc
https://www.digitalocean.com/community/tutorials/an-introduction-to-closures-and-currying-in-javascript#what-is-currying

Top comments (0)