๐ Frontend DSA 7 - ๐งฎ Currying in JavaScript: Create a Sum Function with Infinite Calls Ending with ()
Currying is a powerful and elegant technique in JavaScript used to transform functions with multiple arguments into a sequence of functions, each taking a single argument.
In this guide, weโll build a recursive curried sum function that allows you to call:
sum(1)(2)(3)(4)(5)(); // โ 15
Letโs understand how this magic works. ๐งโโ๏ธโจ
โ Goal
We want to define a function sum()
such that:
sum(0)(1)(2)(3)(4)(5)(); // โ
Output: 15
It should support:
- Chainable calls:
sum(1)(2)(3)...
- Termination with an empty
()
call - Recursive addition of all arguments
๐ง JavaScript Currying Function โ Final Code
function sum(x) {
// Define a nested function that continues the chain
function next(y) {
// If y is passed (truthy), continue summing recursively
if (y !== undefined) {
return sum(x + y); // recursive accumulation
}
return x; // Termination: return the total sum
}
return next;
}
// ๐งช Test
console.log(sum(0)(1)(2)(3)(4)(5)()); // Output: 15
console.log(sum(5)(5)(5)()); // Output: 15
console.log(sum(10)()); // Output: 10
Problem Statement
๐ Explanation
Step-by-step breakdown of sum(0)(1)(2)(3)(4)(5)()
:
sum(0)
returns a functionnext
next(1)
returnssum(0 + 1)
โsum(1)
- Then
sum(1)(2)
โsum(3)
- ...
- Finally, the last call is
sum(15)()
wherey === undefined
- It returns
15
โ ๏ธ JavaScript Note
if (y)
In your original code, this could be problematic if one of the inputs is 0
(because 0
is falsy). A safer condition would be:
if (y !== undefined)
Otherwise, something like sum(0)(0)(1)(2)()
would not behave correctly. โ
๐ก JavaScript Interview Insight
This type of problem is popular in JavaScript interviews to test:
- Closures
- Currying
- Recursion
- Function chaining
- Edge case handling (
0
,undefined
,null
, etc.)
๐งช More Test Cases
console.log(sum(1)(2)(3)()); // 6
console.log(sum(5)(10)(-3)()); // 12
console.log(sum(0)(0)(0)(1)()); // 1
console.log(sum(100)(200)()); // 300
๐ฆ Bonus: ES6 Arrow Version
const sum = x => y => y !== undefined ? sum(x + y) : x;
๐ฌ Want More?
Would you like to:
- Extend this to multiply values? ๐ค
- Collect values in an array instead of summing? ๐
- Build a curried logger or validator function?
I can help with all of them โ just say the word! ๐
Hey there from LearnYard! ๐
Glad to see you exploring the free content from our course! Weโve shared 10 valuable articles at no cost to help you get a real feel of whatโs inside.
If youโve found these helpful, weโd love to know! ๐ฌ
Share your thoughts or key takeaways on LinkedIn, Twitter, or Instagram โ and donโt forget to tag Gourav Hammad & Mohammad Fraz. Weโll personally respond to your post!
Thanks for being part of the LearnYard journey. Keep learning, keep growing! ๐
Loved the free content? Want access to the full course โ for free? ๐ธ
Share the course with your friends using your affiliate link (youโll find it on your profile or home page), and earn 20% commission on each sale.
Just 4โ5 sales and youโll recover the full course price of your course ๐
So why wait? Start sharing, start earning, and unlock the rest of the course effortlessly! ๐ ๏ธ๐
๐ Keywords for SEO
- JavaScript Currying Function Example
- How to Create
sum(1)(2)(3)...()
in JavaScript - JavaScript Closure and Recursion Chaining
- Implement Recursive Currying Function in JS
- Chainable JavaScript Sum Function with Termination
- Understanding Currying in JavaScript
- JavaScript Functional Programming Example
- Currying with Closures and Recursion
- Lazy Evaluation in JavaScript
- Advanced JavaScript Function Chaining Techniques
๐ข Hashtags for Visibility
#JavaScriptCurrying #FunctionChaining #ClosuresInJS #JavaScriptRecursion
#ChainableFunctions #AdvancedJavaScript #LearnJavaScript #CodeNewbie
#JSFunctionalProgramming #FrontendMasters #RecursiveFunctions #JSInterviewPrep #LazyEvaluationJS #JavaScriptTips #FrontendDevelopment #TechLearning #JavaScriptChallenges #CurryingWithClosures #JSCodingPatterns #SumFunctionJS