The callback function of forEach() should not return a value uselessly

  • BAD_RETURN_IN_FOREACH
  • Error
  • Medium
  • No tags

This rule applies when the callback function of forEach() returns any value.

forEach() always ignores the return value of the callback function.

To pass a return value to an external function within the callback function, it is necessary to pass the value through a variable defined in the external function.

Noncompliant Code Example

View with compliant examples side by side
function foo(a) {
  a.forEach(e => { // BAD_RETURN_IN_FOREACH alarm
    if (e.active) {
      return e.data; // This return value is ignored
    }
  });
  return null;
}

Compliant Code Example

View with noncompliant examples side by side
function foo(a) {
  let data = null;
  a.forEach(e => {
    if (e.active) {
      data = e.data;
    }
  });
  return data;
}

Version

This rule was introduced in DeepScan 1.13.0-beta.

See

Was this documentation helpful?