122
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
Conqueror
  • 4,265
  • 7
  • 35
  • 41
  • 2
    It's not anonymous functions, but arrow functions that don't have the `arguments` object. So, your question is making an incorrect statement/assumption. Regular anonymous functions declared with the `function` keyword have the `arguments` object just fine. – jfriend00 Jan 19 '17 at 00:44
  • 3
    Questions about language design decisions are generally not a good fit for Stack Overflow. – Felix Kling Jan 19 '17 at 00:45
  • 9
    @FelixKling. Sorry, I hold the opposite idea against yours. The answer to this question helps remove the "question mark" for a lot of other people. I believe it's never bad to ask a question that looks "not fit" for Stack Overflow. – daiyanze Dec 03 '19 at 08:27
  • @daiyanze: It's not about the question itself but rather who can answer it. Especially for JavaScript, language design questions can only be answered by people who are actually part of the design process, or by those who follow it very closely. There are not many of these people and even fewer are on Stack Overflow. You will likely have more success in getting an answer by reaching out to the relevant people directly (and then maybe post a self-answered question here if it's interesting for more people). – Felix Kling Dec 03 '19 at 09:25
  • I too am with @daiyanze. Given the activity on this question alone, it is clear that this was a good place to bring it. – Mugabo Dec 05 '21 at 03:46

1 Answers1

190

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);
Saksham
  • 9,037
  • 7
  • 45
  • 73
Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • 3
    Good point about how rest arguments obviate the need for the `arguments` object. With rest arguments, you can have an actual array anytime you actually want it. bit there's no language overhead when it isn't used. – jfriend00 Jan 19 '17 at 00:47
  • 2
    Thanks using the rest operator seems to work well. – Conqueror Jan 19 '17 at 01:15
  • 1
    I think in this context, `...arguments` denotes *rest parameters*, not *rest arguments*. See [*Array.of(...items)*](http://ecma-international.org/ecma-262/7.0/index.html#sec-array.of) where the term "rest arguments" is used, whereas in [*String.fromCharCode ( ...codeUnits )*](http://ecma-international.org/ecma-262/7.0/index.html#sec-string.fromcharcode) the term "rest parameters" is used. – RobG Jan 19 '17 at 02:14
  • @RobG Yes, in sigular. The spec differentiates between what is passed, which they call arguments, and how it's bound, which they call parameters. In the dictionary however the two words mean the same so I bet you'll find that the spec actually defines these two in the same document to be able to differentiate between application of a function and the formal parameters. – Sylwester Jan 19 '17 at 10:51
  • 2
    This is the best solution. Your last fancy example indeed fancied me. var fapply = (fun, args) => fun(...args); – Merin Nakarmi Feb 06 '18 at 03:42
  • 23
    Well this sucks, I use `arguments` in `console.log` statements and so using 'rest parameters' would force me to change the function call signatures; It was a bad decision to remove `arguments` from arrow functions. – Daniel Sokolowski Apr 24 '18 at 14:39
  • @DanielSokolowski, well, as you know the arrow functions aren't the same as 'function' which is just a function without link to a context, its context is the caller... As for arrow functions, you assign/call a pure behavior, but from its context, so from the root of a js script you can assign an arrow function to a variable with a log of 'arguments', invoke it and see the Node instance's arguments logged... But from another scoped context, it'd be undefined. – Cerberus May 27 '19 at 19:57
  • 1
    Please note that the first example gives the error `Error: Unexpected eval or arguments in strict mode` in the strict mode, in case you want to use the keyword `arguments`. – Orkun Tuzel Jun 24 '19 at 12:15