5 Advanced JavaScript concepts that will make you a better developer
Currying
Currying means evaluating functions with multiple arguments and decomposing them into a sequence of functions with a single argument. So, instead of taking all arguments at once, the function takes the first argument and returns a new function, which takes second argument and returns a new function, which takes the third… and so on until all arguments are provided and the final function is executed.
Currying helps you divide functions into smaller reusable functions that handle a single responsibility. This makes your functions more pure, less prone to errors and easier to test.
In the above example, we created our own simple implementation for currying a function with exactly three parameters. As a general solution, I suggest using Ramda or similar which supports currying functions with any number of arguments and also with support for changing order of arguments using placeholders.
Composition
Composition is a technique where the result of one function is passed on to the next function, which is passed-on to the next function, and so on… until the final function is executed and some result is computed. Function compositions can be composed of any number of functions.
Composition also helps split functions into smaller reusable functions the has a single responsibility.