Skip to content

Commit

Permalink
docs: elaborate on benefits/restrictions of for-of loops in `no-array…
Browse files Browse the repository at this point in the history
…-for-each` rule doc
  • Loading branch information
bmish committed Mar 24, 2021
1 parent c5a4345 commit c198fdc
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion docs/rules/no-array-for-each.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Prefer `for…of` over `Array#forEach(…)`

A [`for…of` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) is more readable than [`Array#forEach(…)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach).
Benefits of [`for…of` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) over [`Array#forEach(…)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) can include:

* Better readability
* No additional scope, can mean easier debugging
* Ability to early exit with `break` / `return`

This rule is partly fixable.

Note that this rule may erroneously catch/fix loops with arrays that are not iterable and thus cannot be converted to for-of loops.

## Fail

```js
Expand Down

0 comments on commit c198fdc

Please sign in to comment.