Lint Rules

noForEach (since v12.1.0)

Prefer for...of statement instead of Array.forEach.

Here’s a summary of why forEach may be disallowed, and why for...of is preferred for almost any use-case of forEach:

Examples

Invalid

els.forEach(el => {
  el
})
nursery/noForEach.js:1:1 lint/nursery/noForEach ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Prefer for...of instead of Array.forEach
  
  > 1 │ els.forEach(el => {
   ^^^^^^^^^^^^^^^^^^^
  > 2 │   el
  > 3 │ })
   ^^
    4 │ 
  
els['forEach'](el => {
  el
})
nursery/noForEach.js:1:1 lint/nursery/noForEach ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

   Prefer for...of instead of Array.forEach
  
  > 1 │ els['forEach'](el => {
   ^^^^^^^^^^^^^^^^^^^^^^
  > 2 │   el
  > 3 │ })
   ^^
    4 │ 
  

Valid

for (const el of els) {
  el
}