What is the currying function in JavaScript?

Currying is a technique in functional programming that performs the transformation of a function with multiple arguments into several functions containing a single argument in a sequence.

The translation of a function happens something like this

function dummy(param1, param2, param3, .....) {
}

is converted into –

function curriedFunction(param1) {
   return function (param2) {
      return function (param3) {
      }
   }
}

In the curried function, we simply wrap a function inside a function. It means the return of a function is processed by the above function to obtain this kind of translation. The parent function takes the first provided argument that returns the function taking the next argument. This keeps on repeating until the number of arguments ends. Hopefully, the function that receives the last argument returns the expected result.

Example 1

In the below example, we are finding the volume of a box containing 3 variables i.e. length, breadth, and height. The function is called consequently with the parameters that provide arguments and return the appropriate result.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>CapsLock Detecter</title>
</head>
<body>
   <h1 style="color: red;">Welcome To FirstEnquiry</h1>
   <script type="text/javascript">
      function calculateVolume(length, breadth, height) {
         return length * breadth * height;
      }
      console.log("Volume of the container is: " +calculateVolume(4,5,6));
   </script>
</body>
</html>

Output

It will produce the following output in the console.

Volume of the container is: 120

Example 2

In the below example, we are going to convert the above function into a curried function and then pass the required variables to it. On passing the variables, we can see that both the function returns the same output as it should be.

# index.html

<!DOCTYPE html>
<html lang="en">
<head>
   <title>CapsLock Detecter</title>
</head>
<body>
   <h1 style="color: red;">Welcome To FirstEnquiry</h1>
   <script type="text/javascript">
      function calculateVolume(length) {
         return function (breadth) {
            return function (height) {
               return length * breadth * height;
            }
         }
      }
      console.log("Volume of the container is:" + calculateVolume(4)(5)(6));
   </script>
</body>
</html>

Output

It will produce the following output in the console

Volume of the container is: 120

Related Posts

What are custom events in JavaScript?

Custom events are the events that allow you to decouple the code you want to run after a specific piece of code runs. There are various in-built events…

How to use nested for loop in JavaScript?

We use the for loop statement of JavaScript for repeating a set of statements inside the loop body a specified number of times. A nested for loop, as the…

What are the basic rules for JavaScript parameters?

A JavaScript function is a code that performs a particular task. The function parameters are the name list in the function definition. Parameters are also known as…

How to stop refreshing the page on submit in JavaScript?

Using event.preventDefault() to stop page refresh on form submit In this section, we will see how to use event.preventDefault() to stop page refresh on form submission. The event.preventDefault() restricts the default…

Target a Window Using JavaScript or HTML

TARGET attribute of HTML A link’s opening named frame or window is specified using the Target attribute of the <a> anchor tag. The concluding </a> tag in…

What is the role of deferred scripts in JavaScript?

Since JavaScript is a loosely typed language, you are not required to correctly predict the kind of data that will be kept in a variable. Depending on the information…