Skip to content

Latest commit

 

History

History
45 lines (35 loc) · 802 Bytes

no-array-for-each.md

File metadata and controls

45 lines (35 loc) · 802 Bytes

Prefer for…of over Array#forEach(…)

A for…of statement is more readable than Array#forEach(…).

This rule is partly fixable.

Fail

array.forEach(element => {
	bar(element);
});
array.forEach((element, index) => {
	bar(element, index);
});
array.forEach((element, index, array) => {
	bar(element, index, array);
});

Pass

for (const element of array) {
	bar(element);
}
for (const [index, element] of array.entries()) {
	bar(element, index);
}
for (const [index, element] of array.entries()) {
	bar(element, index, array);
}