How to Use Shorthand Functions with Multiple Parameters in JavaScript?

Estimated read time 2 min read

In JavaScript, you can use shorthand functions, also known as arrow functions, to write more concise and readable code. Arrow functions are particularly useful when working with functions that have multiple parameters.

Here’s an example of how you can use an arrow function with multiple parameters in JavaScript:

let multiply = (a, b) => a * b;
console.log(multiply(5, 10));

In this example, an arrow function is used to define a multiply function that takes two parameters, a and b, and returns the result of a * b. The function is called with the arguments 5 and 10, and the result of the function, 50, is logged to the console.

You can also use arrow functions with multiple parameters to simplify more complex functions that return an expression:

let addAndMultiply = (a, b, c) => (a + b) * c;
console.log(addAndMultiply(2, 3, 4));

In this example, an arrow function is used to define an addAndMultiply function that takes three parameters, a, b, and c, and returns the result of (a + b) * c. The function is called with the arguments 2, 3, and 4, and the result of the function, 20, is logged to the console.

Arrow functions can make your code more concise and readable, especially when working with functions that have multiple parameters. However, it’s important to use them appropriately and not overuse them, as they can make your code more difficult to understand for other developers who are not familiar with this syntax.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply