Search code examples
javascriptargumentsarrow-functions

Why this arrow function doesn't work with 'arguments.length'?


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);
// Uncaught ReferenceError: arguments is not defined

Thank you in advance.


Solution

  • 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