What is Currying in Javascript
Currying is one of the advanced techniques to work with functions. Let's see what is currying and how to write curry functions in Javascript.
Currying transforms the way of calling function with multiple parameters to a sequence of single parameter functions.
function multiply (x) {
return function (y) {
return function (z) {
return x * y * z;
};
};
}
console.log(multiply(2)(3)(4))
You can run the above program https://onecompiler.com/javascript/3xmdzm3uj to find the result.
The above code can be written in a more concise way using arrow functions like below:
let multiply_curried = (a) => (b) => (c) => {
return a * b * c;
}
console.log(multiply_curried(2)(3)(4));
You can run the above program https://onecompiler.com/javascript/3xme2jw8s to find the result.