JavaScript

What is Currying Function in JavaScript?

JavaScript supports a variety of mathematical programming functions to optimize operations during web development. These programming functions are utilized in complex problems such as currying.

Most websites utilize the currying function to switch complex tasks to simpler ones. Like other programming languages, JavaScript supports the currying function to resolve different problems.

The content of the article is enlisted here to discuss the currying function in JavaScript.

What is Currying?

Currying is the modern concept of a programming language. Basically, it breaks down the function having multiple arguments and returns a sequence of functions. These functions accept only a single argument at a time. It is very useful to avoid passing the same variables repeatedly.

Additionally, there are various advantages to utilizing the currying function in JavaScript. Some of them are as follows:

  • Division of a complex function into smaller/modular functions.
  • Reduce the errors by the clarity of the code.
  • Restrict passing of the same variables repeatedly.
  • It is useful to create a high-order function.

Difference Between Currying and Traditional Function in JavaScript

The syntax of a currying function is utilized from a traditional function to a modern function strategy. The purpose of this example is to differentiate between them in JavaScript. The syntax for utilizing the simple function in JavaScript is as follows.

Syntax of tradition function in JavaScript

function run(argument1, argument2, argument3) {

   // write the code here

}

In the traditional function, all the arguments are predefined and are used at once while declaring the function.

Currying function in JavaScript.

functionrun(argument1) {
return (argument2) => {
return (argument3) => {
return run(argument1, argument2, argument3)
    }
  }
}

Currying is creating the nested functions based on the number of arguments. In the above syntax, each function returns the argument.

How to Use Currying Function in JavaScript?

In this example, a simple addition operation is performed between three variables, x, y, and z, by utilizing the currying function in JavaScript.

// An example of using the Currying function
const addition =(x) => {
return (y)=>{
return (z)=>{
return x+y+z
        }
    }
}
console.log(addition(12)(10)(5))

The description of the above code is outlined below:

  • An addition function is defined that takes one argument, x.
  • After that, it returns a function that requires an argument, y.
  • The same procedure is repeated with the z argument and returns the sum of these three variables.
  • In this way, the function is not completed until it accepts all the arguments that are passed to it.

Output

The output displays the execution of the above code and returns “27”, which is the sum of 12, 10, and 5 values.

Conclusion

The currying function allows you to break the function into multiple modules to serve one purpose. This strategy makes a currying function more effective than traditional functions. Here, you have learned the working and usage of the Currying function in JavaScript. A simple example and the advantages of the currying function are provided for understanding this function.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.