What is Currying Technique of Javascript

Posted By : Rajan Rawat | 22-Nov-2018

Currying in JavaScript

Currying is the basic tool for functional programming. Functional programming is a programming model that attempts to minimize the number of changes in program state (called side effects) by using immutable data and pure functions (without side effects).

We are going to learn the following things in this post

  • What is currying
  • Why it’s so useful for us
  • How to convert an existing function into a curried version

What is currying?

Currying is a new mechanism or technique which is used for evaluating the functions with the multiple arguments into a sequence of a function with a single argument

Currying is a procedure with multiple arguments and returns a series of functions that take a single argument and eventually parse it into a value.

This is a simple example of using the Lodash _.curry function to illustrate it (we will build our own curry function soon):

function abc(x, y, z) {
return x * y * z;
}
const currying = _.curry(abc);
abc(5, 6, 7); // 210
currying(5)(6)(7); // 210
        

The volume of the original function abc has three parameters, but in curry, we can pass each parameter to three nested functions.

In other words, the curry effectively does the following thing

 function abc1(length) {
  return function(width) {
    return function(height) {
      return height * width * length;
    }
  }
}
        

Why it’s so useful us?

  • Currying can help you avoid passing the same variables over and over again.
  • Help create higher-level features. It is very useful when dealing with events.
  • Widgets can be easily configured and reused.

Let's look at a simple function to add. Accepts three operands as arguments and returns the sum of the three operands.

function addition(x,y,z){
 return x + y + z;
}
        

Now you can call the function with too few results like odd results or too many results in which excess arguments will be ignored

addition(5,6,7) // 18 
addition(5,6) // NaN
addition(5,6,7,8) // 10 --> Extra parameters are ignored.
        

How to convert an existing function into a curried version?

Native JavaScript does not have the curry function so for that you import an external library like lodash to make this possible and convert a native function into a curried function

//import or load lodash
var xyz = function(x, y, z) {
  return x + y + z;
};
 
var currying = _.curry(xyz);
var add = currying(2);

console.log(add(0,0));
// => 2
console.log(add(1,1));
// => 4
console.log(currying(7)(8)(9));
// => 24

        

Note:- Please import the lodash library before doing this and run the above code in your browser and see the results in the console of developer tool of  your browser

 

Conclusion

Currying is a very useful feature of JavaScript technology. It allows you to generate a small, easy-to-configure library of functions that are consistent, quick to use, and understandable as you read the code. Adding curry to coding practices will encourage the use of partially applied functions throughout the code, avoiding many possible duplications, and can help you get a better habit of how to name and manipulate function arguments.

About Author

Author Image
Rajan Rawat

Rajan is a UI Developer, having good knowledge of HTML, CSS and javascript. His hobbies are internet surfing and watching sports.

Request for Proposal

Name is required

Comment is required

Sending message..