0

In JavaScript, the arguments keyword is useful for getting information about a function's arguments. It's 'array-like' in that it can be indexed like an array, but Array.isArray will return false on it.

I'm trying to use arguments.length in a function I am exporting via module.exports like so:

module.exports.foo = (args) => {
    console.log(arguments.length); 
}

The problem: arguments.length always prints 5. When I actually go to print arguments itself, I see that it is an array-like object containing the following:

0: an array of things I'm exporting from the current module (in this case, an array of all of my functions.

1: the require function

2: an object containing information about the current module, like what it's exporting, the including module, file paths, etc.

3: he path to the current module

4: the path to the directory containing the current module

The problem is arguments in this context doesn't work like I expect it to. No matter how many arguments I pass in to the function call, it always prints 5.

How do you properly use the arguments keyword in Node.js exported functions?

Vee
  • 729
  • 10
  • 27
  • Either use a `function` instead, or use argument rest syntax instead, `= (...args) => { console.log(args.length)` – CertainPerformance Aug 20 '19 at 01:00
  • Ah, didn't realize it was an arrow function vs function issue! Thanks. That helped. Using the argument rest syntax now. – Vee Aug 20 '19 at 01:03

0 Answers0