org.sonar.l10n.javascript.rules.javascript.S2251.html Maven / Gradle / Ivy
Why is this an issue?
In a for
loop, the update clause is responsible for modifying the loop counter variable in the appropriate direction to control the
loop’s iteration. It determines how the loop counter variable changes with each iteration of the loop. The loop counter should move in the right
direction to prevent infinite loops or unexpected behavior.
This rule raises an issue when the loop counter is updated in the wrong direction with respect to the loop termination condition.
for (let i = 0; i < strings.length; i--) { // Noncompliant: The counter 'i' is decremented, making the loop infinite
//...
}
To ensure the for
loop behaves as expected, you should specify the correct update clause that moves the loop counter in the right
direction based on the loop’s logic and desired outcome.
for (let i = 0; i < strings.length; i++) {
//...
}
Resources
Documentation
- MDN web docs -
for
- Wikipedia - Infinite loop