- What is currying?
- Currying is a functional programming technique in JavaScript where a function that takes multiple arguments is transformed into a sequence of functions, each taking a single argument.
- Curried Functions are constructed by chaining closures by immediately returning their inner functions simultaneously.
- Example 1:
- Original Function.
function add(x, y) {
return x+y;
}
- Curried Version.
function add(x) {
return function(y) {
return x + y;
}
}
- How to use it:
const addFive = add(5); // addFive is now a function that takes one argment & adds 5 to it
console.log(addFive(3)); // Outputs: 8
// or
console.log(add(5)(3)); // Outputs: 8
Questions
Currying Questions Javascript
Resources