Skip to main content

Currying

Currying is the process of transforming a function that takes several arguments into a sequence of functions, each of which takes only one argument. A function f(a, b, c) becomes f(a)(b)(c), so it can be called in parts and partially applied calls can be reused.

Theory

TL;DR

  • Currying splits a multi argument function into a chain of single argument functions.
  • sum(a, b) becomes curriedSum(a)(b).
  • It works thanks to closures: every returned function remembers the values passed so far.
  • It produces partially applied functions that are convenient to reuse: const add10 = curriedSum(10).
  • It simplifies function composition, which is why it is common in functional programming.
  • It improves readability and flexibility, especially in libraries such as Lodash or Ramda.

Quick example

An ordinary function:

javascript
function sum(a, b) { return a + b; } sum(2, 3); // 5

After currying:

javascript
function curriedSum(a) { return function (b) { return a + b; }; } curriedSum(2)(3); // 5

The same thing with arrow functions is shorter:

javascript
const curriedSum = (a) => (b) => a + b; curriedSum(2)(3); // 5

How it works

  • curriedSum(2) returns a new function that remembers the value a = 2.
  • That function then waits for the second argument b.
  • When we call it with 3, it returns the result 2 + 3.

The memory of the first argument comes from the closure: the inner function keeps access to the outer function's scope even after the outer call has finished.

What currying is good for

  1. Reusing partially applied functions

    javascript
    const add10 = curriedSum(10); add10(5); // 15 add10(7); // 17
  2. It simplifies function composition, which is handy in functional programming: a single argument function drops neatly into a chain of transformations.

  3. It improves readability and flexibility, especially in libraries such as Lodash or Ramda, where curried variants of the methods come out of the box.

In one sentence: currying turns f(a, b, c) into f(a)(b)(c) so that it can be called piece by piece and partial calls can be reused.

Currying and partial application

The two are related but not the same. Currying always produces a chain of functions with exactly one argument per step. Partial application fixes any number of arguments at once and returns a function waiting for the rest:

javascript
function sum(a, b, c) { return a + b + c; } // partial application: fix two arguments at once const addTo3 = sum.bind(null, 1, 2); addTo3(3); // 6

The curried version, by contrast, is called one argument at a time: curried(1)(2)(3).

Common mistakes

  • Confusing currying with partial application: currying is strictly one argument per step.
  • Forgetting that each step returns a function rather than a value: curriedSum(2) is a function, so curriedSum(2) + 3 yields a meaningless string.
  • Losing this when currying an object method: arrow functions take this from where they are declared, ordinary ones need bind.
  • Currying everything: for a normal call with all arguments present, a plain sum(a, b) reads better.
  • Assuming currying makes code faster: it creates extra functions and closures, so it is about expressiveness, not speed.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.