Currying with Arrow Functions
You'll learn how arrow functions provide an elegant, concise syntax for writing curried functions in JavaScript.
The Arrow Advantage
While you can manually curry functions using traditional function declarations, arrow functions make currying incredibly compact and readable. Each arrow (=>) naturally represents one level of the curried chain, visually showing the step-by-step parameter collection.
Here's the classic currying pattern with arrows:
const add = a => b => a + b;
This tiny function is fully curried! Let's break it down:
- The first arrow creates a function that takes
a - It returns another function that takes
b - That inner function finally returns
a + b
Using the Curried Function
const add = a => b => a + b;
const addFive = add(5); // Returns a function waiting for b
console.log(addFive(3)); // 8
console.log(addFive(7)); // 12
// Or call it all at once
console.log(add(10)(20)); // 30
Three-Level Currying
The pattern scales beautifully for multiple parameters:
const multiply = a => b => c => a * b * c;
const double = multiply(2);
const doubleThenTriple = double(3);
console.log(doubleThenTriple(5)); // 30
// Or chain directly
console.log(multiply(2)(3)(5)); // 30
Each arrow adds another layer of partial application, waiting for the next argument.
Why This Syntax Rocks
Arrow functions eliminate the ceremony of writing function and explicit return statements. The result is crystal-clear curried functions that show their structure at a glance.
Key Takeaway: Arrow functions (a => b => a + b) provide the cleanest, most concise syntax for currying, with each arrow representing one step in the parameter collection chain.