javascriptlambdaecmascript-6anonymous-functionarguments-object

Why do arrow functions not have the arguments array?


function foo(x) {
   console.log(arguments)
} //foo(1) prints [1]

but

var bar = x => console.log(arguments) 

gives the following error when invoked in the same way:

Uncaught ReferenceError: arguments is not defined

Solution

  • Arrow functions don't have this since the arguments array-like object was a workaround to begin with, which ES6 has solved with a rest parameter:

    var bar = (...arguments) => console.log(arguments);
    

    arguments is by no means reserved here but just chosen. You can call it whatever you'd like and it can be combined with normal parameters:

    var test = (one, two, ...rest) => [one, two, rest];
    

    You can even go the other way, illustrated by this fancy apply:

    var fapply = (fun, args) => fun(...args);