Horje
What Do Multiple Arrow Functions Mean in JavaScript?

In JavaScript, arrow functions provide a concise syntax for writing function expressions. When you use multiple arrow functions in sequence, it typically signifies a series of nested functions, often used for currying or higher-order functions.

These are the following methods to show the multiple arrow functions in JavaScript having different uses:

Currying with Arrow Functions

Currying is a technique where a function returns another function, enabling partial application of arguments. This can lead to more modular and reusable code, especially in functional programming.

Example: In this example, add is a curried function that takes one argument at a time and returns the sum of two numbers.

JavaScript
const add = a => b => a + b;
console.log(add(1)(2));  

Output
3

Higher-Order Functions with Arrow Functions

Higher-order functions are functions that takes other function as arguments or return them as results. Arrow functions can make these patterns more concise.

Example: Here, withLogging is a higher-order function that wraps the add function to add logging before and after its execution.

JavaScript
const add = a => b => a + b;

const withLogging = fn => a => b => {
  const result = fn(a)(b);
  return result;
};

const addWithLogging = withLogging(add);
console.log(addWithLogging(1)(2)); 

Output
3

Handling Multiple Parameters in Nested Arrow Functions

When dealing with multiple parameters, nested arrow functions can be used to create a chain of functions, each handling one parameter.

Example: In this example, add is a series of nested functions, each taking one parameter and ultimately returning the sum of two numbers.

JavaScript
const add = a => b => a + b;
console.log(add(1)(2));  

Output
3

Returning Functions from Functions

Arrow functions can be used to return new functions based on initial parameters, allowing for dynamic function creation.

Example: This example shows the returning functions from a function.

JavaScript
const add = a => b => a + b;
const createAdder = a => b => add(a)(b);

const addFive = createAdder(1);
console.log(addFive(2));

Output
3





Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Convert a JSON String into an SQL Query? How to Convert a JSON String into an SQL Query?
How to Escape a String in JavaScript? How to Escape a String in JavaScript?
JavaScript String bold() Method JavaScript String bold() Method
JavaScript String fontsize() Method JavaScript String fontsize() Method
JavaScript String link() Method JavaScript String link() Method

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15