Why is this an issue?

When you call a function in JavaScript and provide more arguments than the function expects, the extra arguments are simply ignored by the function.

function sum(a, b) {
  return a + b;
}

sum(1, 2, 3); // Noncompliant: The last argument is unexpected and will be ignored

Passing extra arguments in JavaScript is not inherently "bad," but it can lead to some potential issues or confusion if not handled correctly:

While it’s possible to pass extra arguments, it’s essential to note that accessing those extra arguments directly inside the function is not straightforward. One common approach to handling extra arguments is to use the arguments object, which is an array-like object available within all function scopes.

function sum() {
  let total = 0;
  for (let i = 0; i < arguments.length; i++) {
    total += arguments[i];
  }
  return total;
}

sum(1, 2, 3); // Compliant

However, it’s generally recommended to use the rest parameter syntax (...args) or utilize other techniques like the spread operator to deal with variable numbers of arguments in a more readable and maintainable way.

function sum(...args) {
  return args.reduce((a,b) => a + b, 0);
}

sum(1, 2, 3); // Compliant

Exceptions

No issue is reported when arguments is used in the body of the function being called.

function doSomething(a, b) {
  compute(arguments);
}

doSomething(1, 2, 3); // Compliant

Resources

Documentation