prefer-for-of-loops-over-array-methods

Installation
SKILL.md

Summary

Prefer native for (const item of items) loops over Array methods like forEach. While map and filter are acceptable for simple transformations, forEach should never be used.

Rationale

  • Debugging: forEach callbacks create new stack frames, making "Step Over" debugging difficult (it steps into the callback or finishes the loop entirely). for-of keeps execution in the current scope.
  • Performance: for-of is generally faster as it avoids the function call overhead for every element.
  • Control Flow: forEach does not support await, break, or continue in a standard way. for-of handles these naturally.

Guidance

  1. Strict Ban: Do not use Array.prototype.forEach. Always replace it with for-of.
  2. Preference: Use for-of for any logic involving side effects, asynchronous await, or complex bodies.
  3. Exceptions:
    • One-Liners: Simple map and filter chains are allowed for pure data transformations (e.g., const names = users.map(u => u.name)).
    • If the transformation becomes complex or multi-line, prefer for-of with an explicit accumulator.

Examples

Installs
1
First Seen
Apr 5, 2026
prefer-for-of-loops-over-array-methods — mew-ton/coding-guidelines