π Do not use a for loop that can be replaced with a for-of loop.
πΌπ« This rule is enabled in the β
recommended config. This rule is disabled in the βοΈ unopinionated config.
π§ This rule is automatically fixable by the --fix CLI option.
There's no reason to use old school for loops anymore for the common case. You can instead use for-of loop (with .entries() if you need to access the index).
Off-by-one errors are one of the most common bugs in software. Swift actually removed this completely from the language..
This rule is fixable unless index or element variables were used outside of the loop.
// β
for (let index = 0; index < array.length; index++) {
const element = array[index];
console.log(index, element);
}
// β
for (const [index, element] of array.entries()) {
console.log(index, element);
}// β
for (let index = 0; index < array.length; index++) {
const element = array[index];
console.log(element);
}
// β
for (const element of array) {
console.log(element);
}