Sitemap

JavaScript Currying Magic!🌟✨

3 min readJan 28, 2024

--

Hi to all of you! I’m sure this topic will help you understand the delightful topic I have to share with you.

Today, we’re diving into the enchanting world of currying in JavaScript. But don’t worry, we’re going to learn it step by step, just like building the coolest LEGO castle. So, grab your imagination hats, and let’s embark on this magical coding journey!

Simple Currying implementation in JavaScript

Press enter or click to view image in full size

What is Currying?🄘

Okay, imagine you’re making your favorite peanut butter and jelly sandwich. Usually, you grab all the ingredients at once and make your delicious snack. But what if we break it down into steps?

What's currying like?

Currying is like making your sandwich step by step. First, you spread peanut butter on one slice, then add jelly to another. It’s doing things in little parts!

The JavaScript Magic✨

Now, let’s bring this sandwich magic into JavaScript. In JavaScript, functions can be like recipes. Currying helps us break down these recipes into steps.

Press enter or click to view image in full size

Why do we curry functions?

Currying is helpful because it makes our code more organised. Instead of doing everything all at once, we can break it into smaller tasks. Each step, or ā€œnest,ā€ gets us closer to the final result.

Let’s Code a Curried FunctionšŸ˜Ž

Now, let’s write a simple curried function. Imagine we have a function that adds three numbers:

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

Now, let’s curry it!

function curriedSum(a) {
return (b) => {
return (c) => {
return a + b + c;
};
};
}

How does our curried function work?

Get Mukta Kumari’s stories inĀ yourĀ inbox

Join Medium for free to get updates fromĀ thisĀ writer.

Our curried function works like a set of building blocks. You call it with one number, then another, and finally the last one. Each call returns a new function, waiting for the next number.

Behind the Scenes: How Does Currying Work?

Let’s peek behind the curtain. When you curry a function, it’s like unfolding a story. Each call to a curried function returns a new function, eagerly waiting for its moment in the spotlight.

let sum1 = sumCurried(1);
let sum2 = sum1(2);
let result = sum2(3);
console.log(result); // Result: 6

Let’s have some Fun!šŸŽ‰

Now, let’s play with our curried function:

const result = curriedSum(1)(2)(3);
console.log(result); // Output: 6

Why is this fun?

It’s like playing with LEGO blocks! You build up the result step by step. One call for each number, just like adding one LEGO block at a time.

Exploring Currying Limits😤

Now, you might wonder if currying has limits. Well, yes, like all magical things, it has its boundaries.

Press enter or click to view image in full size
currying implementation for js

Is there a limit to currying?

Currying can go as deep as you want, but it might become too complex if you nest at too many levels. It’s like stacking too many LEGO blocks — it can get a bit tricky to manage.

A little more than currying

By definition, currying should convert sum(a, b, c) into sum(a)(b)(c).

But most implementations of currying in JavaScript are advanced, as described: they also keep the function callable in the multi-argument variant.

Summary

Currying is a transform that makes f(a,b,c) callable as f(a)(b)(c). JavaScript implementations usually both keep the function callable normally and return the partial if the arguments count is not enough.

Currying allows us to easily get partials. As we’ve seen in the logging example, after currying the three argument universal function log(date, importance, message) gives us partials when called with one argument (like log(date)) or two arguments (like log(date, importance)).

and my little advise see this Akshay Saini…vid you better understand the concept.

Thanks for reading, hope u better understand now😊.

--

--