JavaScript Interview Question: Currying

JavaScript Interview Question: Currying

Introduction

Have you ever come across a scenario where you passed multiple arguments to a function, but one argument at a time? Currying is a technique that allows you to do exactly that. It’s a unique way to call inner functions where you can pass arguments partially or pass multiple arguments in a function but one argument at a time. In this article, we will explore what currying is, the syntax for implementing currying in JavaScript, and its practical use in coding.

What is Currying?

Currying is a technique where a function with multiple arguments is transformed into a sequence of functions, each with a single argument. This allows you to pass one argument at a time to a function, instead of passing all the arguments at once. Currying is useful when you want to create a new function by partially applying an existing function. The partially applied function can then be called later with the remaining arguments.

Syntax for Implementing Currying

The syntax for implementing currying in JavaScript involves creating a higher-order function that returns an inner function. The inner function takes the first argument and returns another function that takes the next argument until all the arguments are passed. Here is an example of how to implement currying in JavaScript:

function add(x) {
  return function(y) {
    return x + y;
  };
}
console.log(add(5)(6));

In this example, the add function takes one argument x and returns an inner function that takes another argument y. When you call the add function with the first argument, it returns the inner function. You can then call the inner function with the second argument to get the final result.

Practical Use of Currying in JavaScript

Currying has many practical use cases in JavaScript. One of the most common use cases is in creating reusable functions that can be partially applied. This allows you to create a new function with some of the arguments already set, making it easier to reuse the function in different contexts. Here is an example of how to use currying to calculate the discount amount:

function priceCalculation(price) {
  return function(discount) {
    return price * discount;
  };
}

const discountAmount = priceCalculation(300);

console.log(discountAmount(0.5)); // Output: 150
console.log(discountAmount(0.3)); // Output: 90

In this example, the priceCalculation function takes the price as an argument and returns an inner function that takes the discount as an argument. When you call the priceCalculation function with the first argument, it returns the inner function. You can then call the inner function with the second argument to get the discount amount.

Conclusion

Currying is a powerful technique that allows you to create reusable functions with partially applied arguments. This technique is useful when you want to create a new function by partially applying an existing function. It allows you to pass one argument at a time to a function, instead of passing all the arguments at once.


The content of this blog post was inspired by the JavaScript - Marathon Interview Questions Series 2023 course on Udemy