DEV Community

Sathish Kumar
Sathish Kumar

Posted on

JavaScript for Loop (Increment vs. Decrement)

Mastering Loops in JavaScript: For Loops Explained Simply!
​Understanding how loops work is a fundamental skill for every web developer. Today, I broke down the for loop flow logic into two simple scenarios:

​1. Incrementing Loop (Counting Up):
​Initial Value: Starts at i = 0

​Condition: Runs as long as i <= 5

​Step: Increases by 1 (i++)

​Output: 0, 1, 2, 3, 4, 5
Example:
for (let i = 0; i <= 5; i++) {
console.log(i);
}

2.Decrementing Loop (Counting Down):
​Initial Value: Starts at i = 5

​Condition: Runs as long as i > 0

​Step: Decreases by 1 (i--)

​Output: 5, 4, 3, 2, 1
Example:
for (let i = 5; i > 0; i--) {
console.log(i);
}

Top comments (0)