In JavaScript, arrow functions are a concise way to create functions, and they can accept parameters just like regular functions. You can pass parametervalues to arrow functions by specifying them in the parentheses that follow the arrow symbol (=>). Here's the basic syntax for creating arrow functions with parameters:
(param1, param2, ...) => {
// Function body
// You can use param1, param2, and so on within this function
}
Here's an example of an arrow function with parameters:
const add = (a, b) => {
return a + b;
};
console.log(add(5, 3)); // Outputs 8
In this example, the add arrow function accepts two parameters,
a and b, and returns their sum.
You can also create arrow functions with a single parameter without using parentheses, like so:
param => {
// Function body
// You can use param within this function
}
Here's an example:
const square = x => {
return x * x;
};
console.log(square(4)); // Outputs 16
Arrow functions are commonly used for shorter, concise functions and are particularly useful for functions where you need to pass parameters or return values quickly.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In JavaScript, arrow functions are a concise way to create functions, and they can accept parameters just like regular functions. You can pass parameter values to arrow functions by specifying them in the parentheses that follow the arrow symbol (=>). Here's the basic syntax for creating arrow functions with parameters:
Here's an example of an arrow function with parameters:
In this example, the add arrow function accepts two parameters, a and b, and returns their sum.
You can also create arrow functions with a single parameter without using parentheses, like so:
Here's an example:
Arrow functions are commonly used for shorter, concise functions and are particularly useful for functions where you need to pass parameters or return values quickly.