What is currying in JavaScript?

MediumIntermediateJavascript
Preparing for interviews?

Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.

Quick Answer

Currying is a functional programming technique in JavaScript where a function is transformed into a sequence of functions, each taking a single argument. Trade-offs include readability vs reuse, so test partial application and edge cases.

Answer

The Core Idea

Currying transforms a function that takes multiple arguments into a chain of functions, each taking one argument at a time. It allows partial application — creating new functions by pre-filling some arguments.

JAVASCRIPT
// Normal function
function add(a, b, c) {
  return a + b + c;
}

// Curried version
function curriedAdd(a) {
  return function(b) {
    return function(c) {
      return a + b + c;
    };
  };
}

console.log(curriedAdd(2)(3)(4)); // 9
                  

Concept

Explanation

Example

Partial Application

You can call a curried function one step at a time and reuse intermediate results.

const add5 = curriedAdd(5);
add5(2)(3); // 10

Function Composition

Currying makes it easier to build reusable pipelines of small functions.

const multiply = a => b => a * b;
const double = multiply(2);
double(10); // 20

Closures in Action

Each inner function 'remembers' the outer function’s arguments through closure.

curriedAdd(1)(2)(3); // Works because of lexical scope

Key benefits and characteristics of currying.

Modern Short Syntax

You can write curried functions using arrow functions for brevity:

const add = a => b => c => a + b + c;

console.log(add(1)(2)(3)); // 6


Practical scenario
You build an API client where you first fix the base URL, then inject auth, then pass endpoint params: api(baseUrl)(token)(path).

Common pitfalls

      • Losing this context when curried functions call methods.
      • Over-currying for simple cases, which hurts readability.
      • Confusing currying with default parameters or partial application.
Trade-off or test tip
Currying improves reuse but adds layers of functions. Unit-test each partial step to ensure the right value is captured.

Still so complicated?

Think of currying like a vending machine: instead of inserting all coins at once, you insert them one by one — and after the last coin, you finally get your snack.

Summary
  • Currying transforms multi-argument functions into nested single-argument ones.
  • Enables partial application and functional composition.
  • Helps create reusable, modular logic in a clean, declarative way.
  • Commonly used in frameworks like React, Redux, and functional libraries like Lodash or Ramda.
Similar questions
Guides
22 / 61