Currying Function in JavaScript

Level 4213 keystrokes

Currying transforms a function with multiple arguments into a sequence of single-argument functions.

JavaScript code

// Curry helper: takes a 2-arg function and returns nested functions
1const curry = (fn) => (a) => (b) => fn(a, b);
// Simple add function that takes two arguments
2const add = (a, b) => a + b;
// Create curried version of add
3const curriedAdd = curry(add);
// Call with both arguments: 5 + 3 = 8
4console.log(curriedAdd(5)(3));
// Partial application: create add10 by fixing first arg to 10
5const add10 = curriedAdd(10);
// 10 + 5 = 15
6console.log(add10(5));
// 10 + 20 = 30
7console.log(add10(20));

Output

8
15
30
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • (× 14
  • )× 14
  • =× 8
  • ;× 7
  • >× 4
  • .× 3
  • ,× 2
  • +× 1

More JavaScript examples at level 4

← JavaScript typing practice