Sitemap

Currying In Javascript

1 min readMay 28, 2025

--

(Benefits & Real-World Use Cases)

Currying is a technique in functional programming where a function that takes multiple arguments is transformed into a sequence of functions, each taking a single argument.

Simple Example

Let’s say you have a function that adds three numbers:

Get Meera Devi’s stories in your inbox

Join Medium for free to get updates from this writer.

Without Currying:

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

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

With Currying:

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

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

ES6 Arrow Function Version:

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

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

Real-World Use Cases

  1. Authorization Checks
const hasRole = role => user => user.roles.includes(role);

const isAdmin = hasRole('admin');
console.log(isAdmin({ roles: ['user', 'admin'] })); // true

2. UI Event Handlers in React

const handleClick = id => event => {
console.log(`Clicked item ${id}`);
};

// Usage: <button onClick={handleClick(42)}>Click Me</button>

3. Input Validators

const minLength = len => str => str.length >= len;

const isValidUsername = minLength(5);
console.log(isValidUsername("user1")); // true

Helps with Functional Programming

Currying works well with pure functions and higher-order functions — key principles in functional programming.

  • Makes code more modular, composable, and reusable.
  • Better testability
  • Currying breaking down a function of multiple arguments into a series of unary (single argument) functions.
  • Used often in functional programming and React with hooks and event handling.

--

--

Meera Devi
Meera Devi

Written by Meera Devi

0 followers

I am a Senior Front-End Engineer with years of experience and specialized in Front-End Web technologies.