JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Currying & Partial Application

javascript
mid
currying

What is currying and partial application in JavaScript?

currying
partial-application
functional-programming
closures
patterns
Quick Answer

Currying transforms a function that takes multiple arguments into a sequence of functions each taking one argument: f(a, b, c) becomes f(a)(b)(c). Partial application fixes some arguments of a function and returns a new function for the remaining ones.

Detailed Explanation

Currying converts a multi-argument function into nested single-argument functions:

  • add(1, 2) → add(1)(2)
  • Each call returns a new function until all arguments are provided
  • Named after mathematician Haskell Curry

Partial Application fixes one or more arguments:

  • multiply(2, 3) → double = multiply.bind(null, 2) → double(3)
  • Returns a function with fewer parameters

Benefits:

  • Create reusable, specialized functions from general ones
  • Function composition
  • Point-free programming style
  • Event handler factories

Currying vs Partial Application:

  • Currying: always one argument at a time
  • Partial: can fix any number of arguments at once

Code Examples

Currying examplesJavaScript
// Basic currying
const add = (a) => (b) => a + b;
add(1)(2); // 3

const addFive = add(5);
addFive(3); // 8
addFive(10); // 15

// Practical: logging with context
const log = (level) => (module) => (message) =>
  console.log(`[${level}] ${module}: ${message}`);

const errorLog = log('ERROR');
const authError = errorLog('Auth');
authError('Invalid token'); // [ERROR] Auth: Invalid token
authError('Session expired'); // [ERROR] Auth: Session expired

// Curried API request
const request = (method) => (url) => (data) =>
  fetch(url, { method, body: JSON.stringify(data) });

const post = request('POST');
const createUser = post('/api/users');
await createUser({ name: 'Alice' });

Real-World Applications

Use Cases

Event Handler Factories

Creating pre-configured event handlers like onClick(id) that returns (event) => handleClick(id, event)

Middleware Composition

Express/Koa middleware patterns where middleware(config) returns (req, res, next) => ... for configurable behavior

Validation Builders

Creating reusable validators like minLength(5) returning (value) => value.length >= 5 for composable validation

Mini Projects

Validation Library

beginner

Build composable validators using curried functions (minLength, maxLength, matches, isEmail) that chain together

Functional Pipeline

intermediate

Build a pipe function that composes curried transformation functions for data processing workflows

Industry Examples

Redux

connect(mapState)(Component) uses currying to separate configuration from component binding (hooks now preferred, but connect remains a well-known currying example)

Ramda

Functional programming library where all functions are automatically curried for easy composition

Resources

JavaScript.info - Currying

article

MDN - Function.prototype.bind()

docs

Related Questions

What is a closure in JavaScript and why are they useful?

mid
closures
Previous
How do debounce and throttle work, and when would you use each?
Next
What is the difference between ES Modules (ESM) and CommonJS (CJS)?
PrevNext