0

I'm wondering why the following arrow function, named 'countArg2' doesn't work. Is there anybody can explain what's wrong please?


This works
function countArg1() {
    return arguments.length;
}
countArg1(1, 2, 3);   //3

This doesn't work..
const countArg2 = () => arguments.length;
countArg2(1, 2, 3);
 // VM6745:1 Uncaught ReferenceError: arguments is not defined

Thank you in advance.

Suzuran
  • 39
  • 1
  • 7
  • https://stackoverflow.com/questions/41731854/why-do-arrow-functions-not-have-the-arguments-array – Shivam Pal Jan 15 '21 at 10:14
  • Hi Suzuran: It's already answered here :) https://stackoverflow.com/questions/41731854/why-do-arrow-functions-not-have-the-arguments-array – Imran Rafiq Rather Jan 15 '21 at 10:16
  • [Arrow function expressions - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) -> Differences & Limitations -> _"Does not have `arguments`, or `new.target` keywords."_ – Andreas Jan 15 '21 at 10:16

1 Answers1

1

You have to parse the arguments to the arrow function like this

const countArg2 = (...arguments) => arguments.length;
console.log(countArg2(1, 2, 3));
 // VM6745:1 Uncaught ReferenceError: arguments is not defined
 // at mArgs (<anonymous>:1:29)
 // at <anonymous>:2:1
Aalexander
  • 4,987
  • 3
  • 11
  • 34