DEV Community

mmvergara
mmvergara

Posted on

4 1 1 1 1

Currying in JavaScript

before tackling this topic make sure you have a good understanding about closures and higher order functions in general.

Currying is the process of transforming a function that takes multiple arguments into a sequence of functions, each taking one argument at a time. This allows you to create more specialized and reusable functions.

Think of currying like ordering a custom pizza: instead of specifying all your toppings at once, you add them one by one, building up to the final result.

Take a look at this simple example:

// Non-curried function
function multiply(a, b, c) {
  return a * b * c;
}

console.log(multiply(2, 3, 4)); // 24

// Curried version
function curriedMultiply(a) {
  return function (b) {
    return function (c) {
      return a * b * c;
    };
  };
}

console.log(curriedMultiply(2)(3)(4)); // 24
Enter fullscreen mode Exit fullscreen mode

In the curried version, curriedMultiply takes one argument at a time, returning a new function until all arguments are provided. This might seem like extra work, but it opens up a world of flexibility.


What about the use case?

Currying is like having a toolkit where each tool does one thing really well. It allows you to create partially applied functions—functions that are pre-loaded with some arguments and ready to take the rest later. This is especially useful for reusability and composition.

take a look:

// Curried function to calculate shipping costs
function calculateShippingCost(baseCost) {
  return function (weight) {
    return function (distance) {
      return baseCost + weight * 2 + distance * 0.5;
    };
  };
}

const standardShipping = calculateShippingCost(10); // Base cost of $10
const heavyItemShipping = standardShipping(20); // Weight of 20kg

console.log(heavyItemShipping(100)); // Shipping cost for 100km: 10 + (20 * 2) + (100 * 0.5) = 80
Enter fullscreen mode Exit fullscreen mode

calculateShippingCost is a curried function that lets you create specialized shipping calculators. You can reuse standardShipping and heavyItemShipping for different scenarios.


Another example to solidify stuff.

// Curried function to create a logger
function createLogger(prefix) {
  return function (message) {
    return function (timestamp) {
      return `[${timestamp}] ${prefix}: ${message}`;
    };
  };
}

const errorLogger = createLogger("ERROR");
const warnLogger = createLogger("WARN");

console.log(errorLogger("File not found")(new Date().toISOString()));
// [2025-10-15T12:00:00Z] ERROR: File not found

console.log(warnLogger("Low disk space")(new Date().toISOString()));
// [2025-10-15T12:00:00Z] WARN: Low disk space
Enter fullscreen mode Exit fullscreen mode

In this example, createLogger is a curried function that generates specialized loggers. You can create errorLogger and warnLogger with different prefixes, making your logging system more flexible and reusable.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (3)

Collapse
 
jozaco profile image
Joel

Holy molly thanks for your explanation dude. To be honest I’ve never cared about what currying is or when to use it because I’ve never faced a use case as clear as the two you expose here. I’ve seen the light 💡

Collapse
 
mmvergara profile image
mmvergara

Glad it clicked for you! 🚀 thanks for reading!

Collapse
 
kevinluo201 profile image
Kevin Luo

I think use Decorator Pattern may be a less-confusing approach, so we can chain the method calls. Whatever, it's still explained well in your article 👏

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up