Skip to content

useValidForDirection (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"correctness": {
"useValidForDirection": "error"
}
}
}
}

Enforce “for” loop update clause moving the counter in the right direction.

A for loop with a stop condition that can never be reached, such as one with a counter that moves in the wrong direction, will run infinitely. While there are occasions when an infinite loop is intended, the convention is to construct such loops as while loops. More typically, an infinite for loop is a bug.

for (var i = 0; i < 10; i--) {
}
code-block.js:1:5 lint/correctness/useValidForDirection ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ The update clause in this loop moves the variable in the wrong direction.

> 1 │ for (var i = 0; i < 10; i--) {
│ ^^^^^^^^^^^^^^^^^^^^^^^^
2 │ }
3 │

ℹ The variable moves away from the value that ends the loop, so the loop condition may never become false. This can cause an infinite loop.

ℹ Change the update clause so that it moves the variable toward the loop condition, or change the condition.

for (var i = 10; i >= 0; i++) {
}
code-block.js:1:5 lint/correctness/useValidForDirection ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ The update clause in this loop moves the variable in the wrong direction.

> 1 │ for (var i = 10; i >= 0; i++) {
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
2 │ }
3 │

ℹ The variable moves away from the value that ends the loop, so the loop condition may never become false. This can cause an infinite loop.

ℹ Change the update clause so that it moves the variable toward the loop condition, or change the condition.

for (var i = 0; i > 10; i++) {
}
code-block.js:1:5 lint/correctness/useValidForDirection ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ The update clause in this loop moves the variable in the wrong direction.

> 1 │ for (var i = 0; i > 10; i++) {
│ ^^^^^^^^^^^^^^^^^^^^^^^^
2 │ }
3 │

ℹ The variable moves away from the value that ends the loop, so the loop condition may never become false. This can cause an infinite loop.

ℹ Change the update clause so that it moves the variable toward the loop condition, or change the condition.

for (var i = 0; i < 10; i++) {
}