JavaScript Currying

Currying is a technique in JavaScript where a function is transformed into a sequence of functions, each taking a single argument. It allows partial application of a function’s arguments and helps create more reusable and flexible code.

Currying helps you break down a function that takes multiple arguments into a chain of functions that each take one argument. This is useful in functional programming and can simplify complex logic.



What is Currying?

Currying is a way to write functions so that they take one argument at a time instead of all at once.

Instead of giving a function all the values it needs at once, you give one value first, and it gives you back a new function. Then, you give the next value to that new function, and so on. When you’ve given all the values, the function will do its job.

Here’s an example:

javascript
// Non-curried function
function add(x, y) {
    return x + y;
}

// Curried function
function curriedAdd(x) {
    return function(y) {
        return x + y;
    }
}

Instead of passing all arguments at once, you can call the function in steps:

javascript
const addFive = curriedAdd(5);
result = addFive(3);
console.log(result); // Outputs: 8

// Or call it all at once:
result_curried = curriedAdd(5)(3)
console.log(result_curried); // Outputs: 8

What’s Happening Here?

  • The normal add function takes two numbers at once and adds them.
  • The curriedAdd function takes one number (x) and returns a new function waiting for the second number (y).
  • Calling curriedAdd(5) returns a new function that “remembers” the value 5.
  • Calling addFive(3) (the new function) adds 5 and 3, then returns 8.
  • You can also call curriedAdd(5)(3) all at once, and it works the same way.

Example 1: Creating a Personalized Greeting Function

This example uses currying to create a function that takes a greeting first, then a name, and combines them.

javascript
// Curried greeting function
function greet(greeting) {
    return function(name) {
        return `${greeting}, ${name}!`;
    }
}

const sayHi = greet("Hi");
console.log(sayHi("Freddie")); 
// or
console.log(greet("Hello")("Bob")); 

How It Works:

  • greet takes one argument greeting (like "Hello").
  • It returns a new function that takes a name (like "Freddie").
  • The inner function combines both and returns the full greeting message.
  • You can create a personalized greeting by calling greet("Hi") to get a new function, then call it with a name.

Output

Hi, Freddie!
Hello, Bob!

Example 2: Creating a Multiplier Function

This example demonstrates a curried function that multiplies two numbers by taking one number at a time.

javascript
// Curried multiplier function
function multiply(x) {
    return function(y) {
        return x * y;
    }
}

const double = multiply(2);
console.log(double(5)); // 10

// Or call it all at once:
console.log(multiply(3)(4)); // 12

How It Works:

  • multiply takes the first number x and returns a new function.
  • The returned function takes the second number y and returns the product of x and y.
  • You can create a new function like double by calling multiply(2).
  • Calling double(5) returns 10, multiplying 2 and 5.
  • You can also call the function in one go: multiply(3)(4) returns 12.

Output

10
12

Exercises

Try out the following exercises to practice currying in JavaScript:

1. Create a curried function subtract that subtracts two numbers.
javascript
function subtract(a) {
    return function(b) {
        return a - b;
    }
}

console.log(subtract(10)(4)); // 6

2. Convert this regular function to a curried version: function divide(x, y) { return x / y; }
javascript
const divide = x => y => x / y;

console.log(divide(20)(5)); // 4

3. Write a curried function that takes three arguments separately and returns their product.
javascript
const product = a => b => c => a * b * c;

console.log(product(2)(3)(4)); // 24

*Tip: Experiment by changing arguments to see different results!


Frequently Asked Questions

What is currying in JavaScript?

Currying is a technique where a function is transformed into a sequence of functions, each taking a single argument. It helps break down functions and promotes reusability and functional composition.


How is currying different from partial application?

Currying transforms a function so it takes one argument at a time, returning a new function each time. Partial application pre-fills some arguments of a function but does not require the function to take only one argument at a time.


Why should I use currying?

Currying can help you create more flexible and reusable code. It encourages function composition and modular design, making it easier to build complex logic from smaller, simpler functions.


Can I curry a function with more than two arguments?

Yes. You can curry functions with any number of arguments. For example, a function taking three arguments can be written as: const f = a => b => c => a + b + c;


Is currying used in real-world JavaScript development?

Yes. Currying is often used in functional programming, with libraries like Ramda or Lodash/fp. It’s also useful in scenarios like event handling, configuration, or when working with higher-order functions.



What's Next?

Up next, you'll learn about function composition. This is a simple but powerful idea where you combine smaller functions to build more complex ones. It's like snapping together building blocks to make something new. Understanding function composition will help you write cleaner, more organized code.