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
- 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.
- 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.
- 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.
Now let's make this same add function as a Currying function.
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:
- add(5) returns a function that takes the second number.
- add(5)(10) / result1(10) returns a function that takes the third number.
- 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
Real life use case of Currying by accessing elements from a Object.
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.
Here's how the code works:
- The add function is defined to accept a single argument num1.
- Inside the add function, an inner function is returned. This inner function takes an argument num2.
- 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.
- If num2 is not provided (i.e., it's falsy, like undefined or 0), the inner function returns the accumulated num1.
- 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.
- 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.
- 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
Passionate about JavaScript and front-end technologies, with extensive experience in Drupal development, combined with a strong background in aesthetics.
7moThe best explanation about Currying I founded... straightforward with real use case thaaaanks!
Full-Stack Developer (MERN/Next.js/NestJS) | React.js & Tailwind CSS Specialist | Building Scalable Web Applications
9moIt really helped!
Full stack developer | Angular |Nodejs Looking for my next challenge in Europe
11mothank you so much this is so helpfull for understandings
Open To Work
1yyup really help full im just learning about currying this article post really help me thanks
Software Engineer at Medlife.com
1yIf u want you can add some more real time use cases too, that will help the green to understand better