Skip to main content

array-callback-return

Array methods like map(), filter(), and reduce() expect their callback functions to return a value. Forgetting to return a value is usually a mistake.
Rule Type: Problem
Fixable: No

Why This Rule Exists

If you forget to write a return statement in a callback for array methods, it’s probably a mistake. The callback will return undefined which can cause unexpected behavior.

Rule Details

This rule enforces that callbacks used in array methods return a value.

Array Methods Checked

The rule checks callbacks for these array methods:
  • Array.from() / Array.fromAsync()
  • Array.prototype.every()
  • Array.prototype.filter()
  • Array.prototype.find() / Array.prototype.findIndex()
  • Array.prototype.findLast() / Array.prototype.findLastIndex()
  • Array.prototype.flatMap()
  • Array.prototype.forEach() (optional)
  • Array.prototype.map()
  • Array.prototype.reduce() / Array.prototype.reduceRight()
  • Array.prototype.some()
  • Array.prototype.sort() / Array.prototype.toSorted()

Examples

Incorrect Code

Correct Code

Options

This rule accepts an object with the following options:

allowImplicit

Type: boolean
Default: false
When true, allows callbacks to implicitly return undefined with a return statement containing no expression.

checkForEach

Type: boolean
Default: false
When true, this rule also reports forEach callbacks that return a value (since forEach ignores return values).
With checkForEach: true, returning values in forEach callbacks will be flagged as an error.

allowVoid

Type: boolean
Default: false
When true and checkForEach is also true, allows using void operator in forEach callbacks.

Configuration Examples

When Not to Use It

If you don’t want to enforce return statements in array method callbacks, you can disable this rule.
If you want different behavior for forEach vs other array methods, use the checkForEach option to control this.

Known Limitations

This rule checks callback functions of methods with the given names, even if the object is not actually an array. This is unavoidable with static analysis.