Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

What is currying function in JavaScript ?

  • Last Updated : 29 Sep, 2021

It is a technique in functional programming, transformation of the function of multiple arguments into several functions of a single argument in sequence. 
The translation of function happens something like this,

function simpleFunction(param1, param2, param3, …..) => function curriedFunction(param1)(param2)(param3)(….

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

We simply wrap function inside a function, which means we are going to return a function from another function to obtain this kind of translation. The parent function takes the first provided argument and returns the function that takes the next argument and this keeps on repeating till the number of arguments ends. Hopefully, the function that receives the last argument returns the expected result.  

Note: An American mathematician named Haskell Curry developed this technique, that’s why it is called as currying.



Example 1: Let’s say we have the length, breadth, and height of a cuboid and we want to construct a function that can calculate the volume. The function is being called which consequently executes its code by provided arguments and returns the appropriate result, FInally console.log prints the returned value on console.

JavaScript




<script>
    function calculateVolume(length, breadth, height) {
        return length * breadth * height;
    }
    console.log(calculateVolume(4, 5, 6));
</script>

Output:

120

Example 2: This example explains the currying technique with the help of closures. During the thread of execution, the calculateVolume() function will be invoked. Inside there is an anonymous function, receiving a parameter and returning some code. We are exposing our function from another function, so the closure will be created. Closure always contains the function definition along with the lexical environment of the parent, both things remain connected as a bundle. Hence, it does not matter where we invoke them, the all inner functions will always hold access to the variable of their parent.
As soon as we have got the returned result as a function the next argument is ready to be passed, this process will continue till the second last function. Finally, the innermost return keyword returns the expected result.

JavaScript




<script>
    function calculateVolume(length) {
        return function (breadth) {
            return function (height) {
                return length * breadth * height;
            }
        }
    }
    console.log(calculateVolume(4)(5)(6));
</script>

Output:

120



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!