Benefits of for…of
statement over Array#forEach(…)
can include:
- Faster
- Better readability
- Ability to exit early with
break
orreturn
This rule is partly fixable.
array.forEach(element => {
bar(element);
});
array.forEach((element, index) => {
bar(element, index);
});
array.forEach((element, index, array) => {
bar(element, index, array);
});
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);
}