Skip to content

Rule proposal: no-for-loop #49

@sindresorhus

Description

@sindresorhus

Issuehunt badges

There's no reason to use normal for loops anymore for the common case.

The following:

const arr = ['foo', 'bar'];

for (let i = 0; i < arr.length; i++) {
  const el = arr[i];
  console.log(i, el);
}

Can now be written as:

const arr = ['foo', 'bar'];

for (const [i, el] of arr.entries()) {
  console.log(i, el);
}

Off-by-one errors is one of the most common bugs in software. Swift actually removed this completely from the language.

We could probably ignore reverse loops and other cases, but the common case like above is better written in the latter form.

@kevva @SamVerschueren @jfmengels @jamestalmage Thoughts?

futpib earned $100.00 by resolving this issue!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions