Arguments object throws an error when used with arrow functions.
Consider this code -
function getSum() {
return arguments[0] + arguments[1];
}
console.log(getSum(1, 2));
// 3
The result is as expected. But, you cannot do the same using an arrow function.
const getSum = () => {
return arguments[0] + arguments[1];
};
console.log(getSum(1, 2));
// gobbledygook or error
You will see a function definition, or an error depending on the runtime engine. Arrow functions do not provide the binding for arguments object like regular functions.
Just use a rest operator instead of arguments, and it works in both normal and arrow functions.
const getSum = (...args) => {
return args[0] + args[1];
};
console.log(getSum(1, 2));
// 3