What Do Multiple Arrow Functions Mean in JavaScript?
Last Updated :
05 Jun, 2024
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));
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));
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));
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));
Similar Reads
What are these triple dots (...) in JavaScript ? In JavaScript, there are multiple ways in which we can assign the value of one object to another. Sometimes we do not know the exact number of arguments that are to be assigned. In this case, the triple dots are used. The triple dots are known as the spread operator, which takes an iterable(array,
4 min read
Arrow functions in JavaScript An arrow function is a shorter syntax for writing functions in JavaScript. Introduced in ES6, arrow functions allow for a more concise and readable code, especially in cases of small functions. Unlike regular functions, arrow functions don't have their own this, but instead, inherit it from the surr
5 min read
What is the syntax for leading bang! in JavaScript function ? Before we get to know the syntax for the leading bang! in a JavaScript function, let's see what functions in JavaScript are actually are. JavaScript functions are a set of statements(procedures) that perform some tasks or calculate some value. A function may take some input and return the result to
2 min read
What does '...' mean in JavaScript? The '...' (or 3 dot symbol) in JavaScript is known as the Spread operator or Rest operator based on the usage. This syntax is used to expand the iterable into individual elements such as arrays, objects, etc.Syntax for Spread Operatorcosnt a1 = [ 10, 20, 30, 40 ];const a2 = [ ...a1, 50]; // Extracti
4 min read
What are Arrow/lambda functions in TypeScript ? Arrow functions in TypeScript (also known as lambda functions) are concise and lightweight function expressions using the => syntax.Provide a shorter syntax for defining functions.Automatically bind the context of their surrounding scope.It is commonly used for callbacks, array methods, and simpl
3 min read