unicorn/no-for-loop Style ​
What it does ​
Enforces the use of for-of
loops over classical counter based for
loops.
Why is this bad? ​
- A
for-of
loop makes intent explicit – iterate over all elements. - It removes boiler-plate (
i
,arr.length
, manual++
).
Examples ​
Examples of incorrect code for this rule:
js
for (let i = 0; i < items.length; i++) {
const item = items[i];
console.log(item);
}
Examples of correct code for this rule:
js
for (const item of items) {
console.log(item);
}
How to use ​
To enable this rule in the CLI or using the config file, you can use:
bash
oxlint --deny unicorn/no-for-loop
json
{
"rules": {
"unicorn/no-for-loop": "error"
}
}