Currying means turning a function that takes several arguments into a chain of functions that each take one argument. Instead of calling add(2, 3) , you call add(2)(3) . It looks strange at first, but it makes reusable, specialised functions almost free.
// Normal function function add ( a , b ) { return a + b ; }
// Curried version function curriedAdd ( a ) { return function ( b ) { return a + b ; } ; }
const addFive = curriedAdd ( 5 ) ; console . log ( addFive ( 3 ) ) ; // 8 console . log ( addFive ( 10 ) ) ; // 15
Enter fullscreen mode
Exit fullscreen mode
addFive is a new function with the first argument already filled in. This is called partial application, and it is the real reason currying exists. You configure a function once, then reuse the specialised version everywhere.
A common real-world use is logging. One function, configured for each level:
function log ( level ) { return function ( message ) { console .…