Currying in JavaScript

Currying is a technique of translating the evaluating of a function that takes multiple arguments into evaluating a sequence of functions each with a single argument.

const sum = (a, b) => a + b;

// with Currying in JavaScript
const curriedSum = (a) => (b) => a + b;
console.log(curriedSum(3)(2));
5

The function from taking multiple parameters to taking a parameter at a time.

You can create multiple utility functions out of this

const curriedSum = (a) => (b) => a + b;
const sumBy5 = curriedSum(5)
console.log(sumBy5(2));
7

Leave a Reply

Your email address will not be published.

ANOTE.DEV