DEV Community

ski
ski

Posted on

arguments object in arrow functions

intro

normally, when you write a function, you can call upon the arguments array to loop through the arguments passed into the function as i did here with a function constant.

function foo () {
    console.log(arguments);
}
foo(1);
Enter fullscreen mode Exit fullscreen mode

fix

however, (fat) arrow functions () => {} do not possess an arguments object within their scope as the followin' will produce a reference error.

const foo = () => console.log(arguments);
foo(1);
Enter fullscreen mode Exit fullscreen mode

however, the trick is to cleverly employ the spread operator to create a rest parameter. (technically, arrays are objects, so the rest parameter still counts as an arguments object)

const foo = (...args) => console.log(args);
foo(1);
Enter fullscreen mode Exit fullscreen mode

happy codin'!

Top comments (0)