array-callback-return
Array methods likemap(), 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
Fixable: No
Why This Rule Exists
If you forget to write areturn 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: booleanDefault:
false
When true, allows callbacks to implicitly return undefined with a return statement containing no expression.
checkForEach
Type: booleanDefault:
false
When true, this rule also reports forEach callbacks that return a value (since forEach ignores return values).
allowVoid
Type: booleanDefault:
false
When true and checkForEach is also true, allows using void operator in forEach callbacks.