What is Currying in JavaScript?
Currying in JavaScript

What is Currying in JavaScript?

Currying in JavaScript?

At its core, currying is a functional programming technique that involves breaking down a function that takes multiple arguments into a series of functions that take one argument each. This creates a chain of functions, where each function returns another function until the final result is achieved.

Advantages of function Currying in JavaScript

  1. Reusability: Currying breaks down a complex function into smaller, reusable units. Each curried function focuses on a single argument, making it easier to understand and maintain. These smaller functions can be reused across different parts of your codebase.
  2. Partial Function Application: Currying allows you to create partially applied functions, where you fix some of the arguments in advance and leave the rest to be supplied later. This is useful when you have a function that requires some parameters to be the same across multiple calls.
  3. Code Composition: Currying encourages the creation of new functions by composing existing ones. The currying function in JavaScript is a higher-order function. Higher-order functions are those that either take another function as an argument or return a function. It uses functional programming which results in cleaner, more expressive code.

Simple Currying implementation in JavaScript

Let's first create a add function to add 3 arguments.

No alt text provided for this image
Add function in JS

Now let's make this same add function as a Currying function.

No alt text provided for this image
Currying in JS

In this curried version of the add function, each function takes one argument and returns another function until all the arguments are collected, and the final sum is calculated.

Here's how the currying works step by step:

  1. add(5) returns a function that takes the second number.
  2. add(5)(10) / result1(10) returns a function that takes the third number.
  3. add(5)(10)(15) returns the final result, which is the sum of all three numbers.

This way, you can call the curried add function with each argument separately, creating a more modular and composable function.

Currying using ES6 syntax in JavaScript

No alt text provided for this image

Real life use case of Currying by accessing elements from a Object.

No alt text provided for this image
Currying Usecase

Then What is Infinite Currying?

In the following code we have converted the function to have infinite argument using some recursive nature as well. This allows you to create an infinite currying chain, where you can keep adding numbers as needed, and when you're ready to get the final result, just call the curried function with no arguments to compute the sum.

No alt text provided for this image
Infinite Currying

Here's how the code works:

  1. The add function is defined to accept a single argument num1.
  2. Inside the add function, an inner function is returned. This inner function takes an argument num2.
  3. If num2 is provided (i.e., it's truthy), the inner function returns a recursive call to add with the sum of num1 and num2.
  4. If num2 is not provided (i.e., it's falsy, like undefined or 0), the inner function returns the accumulated num1.
  5. When the curried add function is called, it can be immediately invoked with the next argument, and this process can be repeated for as many arguments as needed.
  6. The final invocation of the curried add function is done with an empty set of parentheses (). This results in computing the accumulated sum of all previously provided numbers.
  7. The result is 11, which is the sum of 5 and 6.

Conclusion

Function currying is a programming pattern in which we pass only a few arguments to the function rather than all of them at once. The function returns another function to that we may provide additional parameters. Implementing a curried version of every function is a tedious task, therefore, a currying function is a better option. A currying function takes a function as an argument and returns the curried version of that function. Partial functions are similar to curried functions having a few of their arguments fixed.

This article hopefully provided you with some new information. Share it with your friends if you enjoyed it. Also, please provide your feedback in the comments section.

Thank you so much for reading...

#javascript #frontend #interviewprep #js #frontend Harley Ferguson Brian Jenney Ryan Talbert


Julie Danjou

Drupal ? Javascript ES6 ? Angular v18 ? React v18 ? Vue.js v3

2 个月

The best explanation about Currying I founded... straightforward with real use case thaaaanks!

回复
Golam Rabby

Full-Stack MERN & Next.js Developer | React.js & Tailwind CSS Specialist | Building Scalable Web Applications

4 个月

It really helped!

回复
Aliakbar Esmaeili

Full stack developer | ???Ngular| Nodejs Looking for my next challenge in Europe

6 个月

thank you so much this is so helpfull for understandings

回复
Pratik Jain

Open To Work

8 个月

yup really help full im just learning about currying this article post really help me thanks

Shakeel Ahmed

Software Engineer at Medlife.com

8 个月

If u want you can add some more real time use cases too, that will help the green to understand better

要查看或添加评论,请登录

Arjunan K的更多文章

社区洞察

其他会员也浏览了