Currying is the process of splitting up a function with several parameters into several functions, each with just one argument. Currying is named for Haskell Curry, a mathematician. A n-ary function can become a unary function by currying.

Let's look at an example of an n-ary function and how a currying function is created.

const multiArgFunction = (a, b, c) => a + b + c;
console.log(multiArgFunction(1, 2, 3)); // 6

const curryUnaryFunction = (a) => (b) => (c) => a + b + c;
curryUnaryFunction(1); // returns a function: b => c =>  1 + b + c
curryUnaryFunction(1)(2); // returns a function: c => 3 + c
curryUnaryFunction(1)(2)(3); // returns the number 6

Using closures, this example demonstrates the currying approach. The calculateVolume() function will be used during the execution thread. There is an anonymous function within that takes a parameter and outputs some code. It will be built because we are exposing our function from another function. The lexical environment of the parent and the function definition are always present in the closure and are kept together as a bundle. Therefore, regardless of where we call them, all inner functions will always have access to the parent variable.

The next argument is ready to be given as soon as the returned result has been received as a function; this procedure will continue until the second-to-last function.

The innermost return keyword finally produces the anticipated outcome.

	function calculateVolume(length) {
		return function (breadth) {
			return function (height) {
				return length * breadth * height;

Curried functions are excellent for enhancing functional composition and code reuse.

Recommended Posts

View All

10 JavaScript Tricks Every Developer Should Know

I'll show you several JavaScript tricks for optimizing your JavaScript code. Use these JavaScript tricks to optimize your JavaScript code.

JavaScript Program to Check if An Object is An Array

The function toString method?from Object.prototype is the best approach to determine whether an object is an instance of a given class or not.

5 JavaScript console method you should be aware of

JavaScript includes several fantastic console methods that are less commonly used yet really useful.

What is the difference between Call, Apply and Bind

Looking to learn about the differences between Call, Apply, and Bind in JavaScript? This article breaks down the nuances of each method and provides c...

JavaScript Immediately-invoked Function Expressions (IIFE)

Learn about Immediately-invoked Function Expressions (IIFE) in JavaScript. Understand their benefits and how to use them effectively. Start coding now