Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is currying in JavaScript

The Curry Dish Analogy

Currying is a mouthful of a term, no pun intended. If you're new to programming or JavaScript, you might be wondering what this culinary term has to do with writing code. To help you understand, let's use an analogy. Imagine you're in a restaurant, and you order a curry dish. But instead of serving it all at once, the waiter brings you the ingredients one at a time. First, you get the sauce, then the meat, and finally, the vegetables. This is somewhat similar to what happens when we curry a function in JavaScript.

Defining Currying

In simple terms, currying is a process in functional programming where a function with multiple arguments is transformed into a sequence of functions, each with a single argument. So, if you have a function that takes three arguments, currying would break it down into three functions, each taking one argument.

Currying in Action

Let's look at an example. Here's a simple function that adds two numbers together:

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

If we call add(1, 2), we get 3 as the result. Simple, right?

Now, let's transform this into a curried function.

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

With this curried version, we would call it like this: curriedAdd(1)(2), and we would still get 3.

The Why of Currying

You might be wondering why we would want to do this. It seems a bit more complicated, right? In programming, we often solve complex problems by breaking them down into smaller, simpler problems. Currying allows us to do this by creating smaller, more specific functions from our general ones.

Another Example

Let's look at another example. Suppose we have a function that calculates the total price for a number of items at a specific price:

function totalPrice(price, quantity) {
    return price * quantity;
}

We could call this function with two arguments, like totalPrice(10, 2), and it would give us 20.

Now, let's curry this function:

function curriedTotalPrice(price) {
    return function(quantity) {
        return price * quantity;
    }
}

With this curried version, we would call it like this: curriedTotalPrice(10)(2), and we would still get 20. But now, we can also create a new function that's specifically for items that cost $10:

var tenDollarItems = curriedTotalPrice(10);

Now, we can easily calculate the total price for any quantity of $10 items, like tenDollarItems(2), which gives us 20.

Conclusion: The Spice of JavaScript

Currying, like the spice it's named after, adds a unique flavor to JavaScript programming. It might seem a little strange at first, but once you get the taste for it, you'll find it adds a depth and complexity to your code that's hard to achieve in other ways. Currying allows you to break down complex problems into simpler, more manageable pieces, and can make your code more readable and easier to debug. So next time you're in the coding kitchen, don't be afraid to add a little curry to your JavaScript dish. Who knows, you might find it's just the ingredient you've been missing.