← JavaScript Interview Questions

What is currying in JavaScript?

2
1
Asked 5 years ago by Hayk Hovhannisyan

Answers

Currying is technique evaluating a function with multiple arguments into sequence of functions with a single argument.

In other words, instead of taking all arguments at once, the function takes an argument an returns a function which receives the next argument and etc.

Example:

Here is a simple add function which we are going to modify:

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

Here is the same function with carrying approach:

function add(a) {
  return (b) => {
    return (c) => {
      return a + b + c;
    }
  }
}
add(1)(2)(3);

0
Answered 5 years ago by Hayk Hovhannisyan