Arrow Functions
What is the output when passing extra arguments to an arrow function?
const add = (a, b) => a + b;
console.log(add(2, 3, 4, 5));
The output is `5`. This demonstrates how JavaScript handles extra arguments passed to a function. When more arguments are provided than parameters defined, the extra arguments are simply ignored. The arrow function `add` is defined with two parameters, `a` and `b`, which receive the values `2` and `3` respectively. The additional arguments `4` and `5` are passed to the function but not assigned to any named parameters, so they have no effect on the function's execution. The function calculates `2 + 3` and returns `5`. This behavior is the same for arrow functions, function expressions, and function declarations. If you need to work with a variable number of arguments, you can use rest parameters (`...numbers`) to collect all arguments into an array. For example, `const sum = (...numbers) => numbers.reduce((total, n) => total + n, 0);` would sum all provided arguments. Understanding how JavaScript handles function arguments is important for writing robust code, especially when working with functions that might be called with varying numbers of arguments.