Blog/NotesConcept

Implement Infinite Currying Sum: JavaScript Interview Question sum(1)(2)(3)

In this post, we will going to cover the step-by-step implementation of Infinite Currying Sum with a code example. This is one of the most common JavaScript Interview questions.

Intermediate

Anuj Sharma

Last Updated Oct 26, 2025


Infinite currying sum and multiplication are one of the best examples to check the understanding of currying and its implementation to generate partial functions. In this post, we will going to cover the infinite currying sum implementation.

Table of Contents

  1. Implementing Infinite Currying Sum
  2. Real-world example

Implementing Infinite Currying Sum

Let's start by implementing a function that can take any number of arguments and return the sum of all those arguments. We will use currying to achieve this.

Step 1: Define the Base Function

function sum(...args) {
    return args.reduce((acc, val) => acc + val, 0);
}
 

The above function takes any number of arguments using the rest parameter syntax (...args) and uses the reduce method to sum them up.

Step 2: Implement Currying

function curry(func) {
    return function curried(...args) {
        if (args.length >= func.length) {
            return func(...args);
        } else {
            return function (...nextArgs) {
                return curried(...args, ...nextArgs);
            };
        }
    };
}

const curriedSum = curry(sum);
 

In the curry function, we check if the number of arguments passed is equal to or greater than the number of arguments expected by the sum function. If not, we return a new function that takes the remaining arguments. This process continues until all arguments are collected and the sum is calculated.

Step 3: Using Infinite Currying

console.log(curriedSum(1)(2)(3)()); // Output: 6
console.log(curriedSum(5)(10)(15)(20)()); // Output: 50
 

By invoking the curriedSum function with a series of arguments followed by an empty function call, we can achieve infinite currying and calculate the sum of all the arguments provided.

Real-World Example

Let's consider a real-world scenario where currying can be useful. Suppose in the e-commerce application, we have a function that calculates the total price of a shopping cart with discounts applied based on the customer's membership level:

function calculateTotalPrice(discount) {
    return function (price) {
        return price - price * discount;
    };
}

const standardMemberPrice = calculateTotalPrice(0.1);
const premiumMemberPrice = calculateTotalPrice(0.2);

console.log(standardMemberPrice(100)); // Output: 90
console.log(premiumMemberPrice(100)); // Output: 80
 

In this example, the calculateTotalPrice function returns a new function that calculates the final price after applying the discount based on the membership level. This demonstrates how currying can be used to create specialized versions of a generic function.

Final Thoughts

Implementing infinite currying in JavaScript can be a powerful technique to create flexible and reusable functions. By breaking down functions into smaller units that can be partially applied, developers can achieve more expressive and concise code.

Understanding currying is not only important for technical interviews but also for writing clean and maintainable code in frontend development.

Learn Next

Implement Currying Multiplication


Share this post now:

💬 Comments (0)

Login to comment

Advertisement

Flaunt You Expertise/Knowledge & Help your Peers

Sharing your knowledge will strengthen your expertise on topic. Consider writing a quick Blog/Notes to help frontend folks to ace Frontend Interviews.

Advertisement


Other Related Blogs

Polyfill for map, filter, and reduce in JavaScript

Anuj Sharma

Last Updated Oct 2, 2025

Explore Polyfill for map, filter and reduce array methods in JavaScript. A detailed explanation of Map, filter and reduce polyfills in JS helps you to know the internal working of these array methods.

Master Hoisting in JavaScript with 5 Examples

Alok Kumar Giri

Last Updated Jun 2, 2025

Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.

Implement useFetch() Custom Hook in React

Anuj Sharma

Last Updated Oct 25, 2025

Find the step-by-step explanation of the useFetch custom hook in React that helps in fetching the data from an API and handling loading, error states.

Understanding popstate event in Single Page Applications (SPAs)

Vijay Sai Krishna vsuri

Last Updated Aug 21, 2025

A Quick guide about popstate event in JavaScript, If you’ve ever hit the back button in your browser and wondered how your Single-Page Application knows which view to render, this guide is for you.

setTimeout Polyfill in JavaScript - Detailed Explanation

Anuj Sharma

Last Updated Aug 3, 2025

Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.

Implement Infinite Currying Multiplication in JavaScript: mul(2)(3)(4)

Anuj Sharma

Last Updated Oct 26, 2025

Understand the step-by-step implementation of Infinite Currying Multiplication in JavaScript with a code example.

Stay Updated

Subscribe to FrontendGeek Hub for the frontend interview preparation, interview experiences, curated resources and roadmaps.

FrontendGeek
FrontendGeek

© 2024 FrontendGeek. All rights reserved