DEV Community

Cover image for Mystery of Function Currying in JavaScript!
Sudhil Raj K
Sudhil Raj K

Posted on • Edited on

Mystery of Function Currying in JavaScript!

Currying. Sounds fancy, right? Like something complicated you only use if you're building a giant functional programming library. But actually, it’s one of those little concepts in JavaScript that’s super useful and surprisingly fun once you get the hang of it!

Let’s break it down the easy way. No scary jargon. Just real talk.

So What Is Currying? 🤔

Normally, when you write a function, it looks like this:

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

add(2, 3); // 5
Enter fullscreen mode Exit fullscreen mode

Currying lets you break that down into smaller pieces — instead of giving all arguments at once, you feed them in one at a time:

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

add(2)(3); // 5
Enter fullscreen mode Exit fullscreen mode

See what happened? add(2) gives you back a new function that’s waiting for b. When you give it 3, the magic happens!

In simple words:

Currying is transforming a function that takes multiple arguments into a series of functions that take one argument at a time.

Why Would Anyone Do That? 🤷‍♂️

I had the same question. Like, why split up arguments like that?

Here’s the deal: currying gives you more control, especially when you want to reuse functions with some preset values.

Example? Say you want to create a function that always doubles a number:

function multiply(a) {
  return function(b) {
    return a * b;
  };
}

const double = multiply(2);
console.log(double(5)); // 10
console.log(double(10)); // 20
Enter fullscreen mode Exit fullscreen mode

Instead of writing multiply(2, x) everywhere, you’ve got a neat double() function ready to go. Handy, right?

Let's Build a Curry Utility! 🛠

Here’s how to make your own curry function that can handle multiple arguments step by step:

function curry(fn) {
  return function curried(...args) {
    if (args.length >= fn.length) {
      return fn(...args); // Got enough arguments, just run the function
    } else {
      return function (...nextArgs) {
        return curried(...args, ...nextArgs); // Keep collecting
      };
    }
  };
}
Enter fullscreen mode Exit fullscreen mode

Now use it like this:

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

const curriedSum = curry(sum);

console.log(curriedSum(1)(2)(3));     // 6
console.log(curriedSum(1, 2)(3));     // 6
console.log(curriedSum(1)(2, 3));     // 6
Enter fullscreen mode Exit fullscreen mode

It’s flexible — and surprisingly elegant!

Real Life Example You’ll Love ❤️

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

const sayHello = greet("Hello");
console.log(sayHello("Sudhil")); // Hello, Sudhil!
console.log(sayHello("Raj"));   // Hello, Raj!
Enter fullscreen mode Exit fullscreen mode

This is where currying shines. You’ve built a greeting factory 🎉

Wait, Isn’t This Just Partial Application?

Great question — they’re related, but not quite the same.

Currying transforms a function into a series of functions that each take one argument.

Partial application lets you pre-fill some arguments, and the result can still take multiple arguments.

Should I Start Currying Everything ❓

Probably not. 😄

Use currying when it makes your code cleaner or more reusable, not just to look cool. In team projects, it’s important that your teammates understand what’s going on too.

That said, for utility functions, config-based behavior, or when you're building reusable patterns — currying is super helpful.

Wrapping It Up 💡

Currying might look a little weird at first, but once you try it out, you’ll see why it’s a favorite among JS developers who love clean, modular code.

Here's What You Should Remember 🧠

Currying breaks a function into a chain of functions that take one argument each.

It helps create reusable and customizable logic.

You don’t need to use it everywhere — just where it makes sense.

Thanks for reading 🙌
Stay curious, and keep experimenting.
Happy Coding! 🚀👨‍💻✨

Top comments (3)

Collapse
 
imthedeveloper profile image
ImTheDeveloper

Love the breakdown.

In all honesty though I'm too low iq still to understand when I would use it. I fully get the example of double and even your greeting. But I just can't see a real use case where I'm not more than happy enough to just add the additional function calls myself.

Collapse
 
sudhil profile image
Sudhil Raj K

Thank you so much @imthedeveloper ! 😊
And honestly, that’s a really common thought, you're not alone at all.

Currying can seem like “extra effort” when you're just as happy writing regular function calls. In most cases, that’s perfectly fine! But currying really shines when you want to preset values and reuse the logic.

Take another example:
Let’s say you're building a form validation system. You might want to reuse the same function to check if different fields have a minimum length.
Instead of doing this:

function validateMinLength(field, length) {
  return field.length >= length;
}
validateMinLength(username, 4);
validateMinLength(password, 8);
Enter fullscreen mode Exit fullscreen mode

You can curry it like this:

const minLength = (length) => (field) => field.length >= length;

const min4 = minLength(4);
const min8 = minLength(8);

min4(username); // true or false
min8(password); // true or false
Enter fullscreen mode Exit fullscreen mode

Now you’ve created mini validator functions like min4 and min8 that can be reused throughout your app or even shared across components.

It's not something you'll use every day, but it's incredibly handy when building reusable utilities.

Appreciate your honest comment, really! 🙌
Thanks Again💖

Some comments may only be visible to logged-in visitors. Sign in to view all comments.