in JavaScript by
Q:
What is currying in JavaScript?

1 Answer

0 votes
by

Currying is an advanced technique to transform a function of arguments n, to n functions of one or less arguments.

Example of a curried function:

function add (a) {

  return function(b){

    return a + b;

  }

}

add(3)(4)

For Example, if we have a function f(a,b) , then the function after currying, will be transformed to f(a)(b).

By using the currying technique, we do not change the functionality of a function, we just change the way it is invoked.

Let’s see currying in action:

function multiply(a,b){

  return a*b;

}

function currying(fn){

  return function(a){

    return function(b){

      return fn(a,b);

    }

  }

}

var curriedMultiply = currying(multiply);

multiply(4, 3); // Returns 12

curriedMultiply(4)(3); // Also returns 12

As one can see in the code above, we have transformed the function multiply(a,b) to a function curriedMultiply , which takes in one parameter at a time.

Click here to read more about JavaScript
Click here to read more about Insurance

Related questions

+1 vote
asked Feb 29, 2020 in JavaScript by RShastri
0 votes
asked Oct 19, 2019 in JavaScript by SakshiSharma
0 votes
asked Feb 6, 2020 in JavaScript by rajeshsharma
0 votes
asked Dec 10, 2020 in JavaScript by SakshiSharma
0 votes
asked Oct 19, 2019 in JavaScript by SakshiSharma
0 votes
...