
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
The curried name is used by the American Mathematician name Huskel Curry who developed this technique.
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.
# index.html
<!DOCTYPE html> <html lang="en"> <head> <title>CapsLock Detecter</title> </head> <body> <h1 style="color: red;">Welcome To Tutorials Point</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 Tutorials Point</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 Articles
- What is currying in JavaScript?
- Currying VS Partial Application in JavaScript.
- Higher Order Functions and Currying in JavaScript
- What is the (function() { } )() construct in JavaScript?
- What is "function*" in JavaScript?
- What is function overloading in JavaScript?
- What is function() constructor in JavaScript?
- What is super() function in JavaScript?
- What is function chaining in JavaScript?
- What is the role of clearTimeout() function in JavaScript?
- What is the use of Math.hypot() function in JavaScript?
- What is the importance of _.initial() function in JavaScript?
- What is the use of apply() function in JavaScript?
- What is the use of Math.imul( ) Function in JavaScript?
- What is a function literal in JavaScript?
