An important difference between the regular and the Arrow functions also involves the "arguments" object:
In JavaScript, the "arguments" object is a special object that is automatically available within the scope of a regular function, allowing you to access the arguments passed to that function, even if they are not explicitly defined as named parameters.
Arrow functions do not have their own "arguments" object. Instead, they inherit the "arguments" object from their containing (surrounding) function or lexical scope.
function regularFunction() {
console.log(arguments); // This works
}
const arrowFunction = () => {
console.log(arguments); // This will not work as expected
};
regularFunction(1, 2, 3); // // Arguments(3) [1, 2, 3, ...]
arrowFunction(1, 2, 3); // Uncaught ReferenceError
In the above example, the "arguments" are not defined in the scope of the "arrowFunction", which leads to the ReferenceError.
function regularFunction() {
const arrowFunction = () => {
console.log(arguments); // Accesses the "arguments" object of the regularFunction
};
arrowFunction();
}
regularFunction(1, 2, 3); // Arguments(3) [1, 2, 3, ...]
In this example, the "arrowFunction" has access to the "arguments" object defined in the scope of the surrounding "regularFunction".