Convert loop to for…of

Replace regular for loops and anArray.forEach loops with for…of loops.

Refactoring (Convert)
Modernization
ES2015
P42 for VS Code
P42 for GitHub
Convert loop to for…of

for...of loops over iterable objects, for example arrays or strings.

It is easier to read than indexed for loops and can replace them in many cases.

Refactoring Example

Before:

for (let i = 0; i < elements.length; i++) {
  const element = elements[i];
  console.log(element);
}

After:

for (const element of elements) {
  console.log(element);
}

Configuration

Related Code Assists

Learn More

Available In