Mastering the for Loop in JavaScript
The for loop is a common way to repeat code. It uses three parts inside the parentheses:
for (Initialization; Condition; Increment/Decrement) {
// code block
}
What each part means:
- Initialization: creates the loop counter (runs once);
- Condition: checked before every iteration;
- Increment/Decrement: updates the counter after each loop.
An iteration is one full execution of the loop body.
123for (let i = 1; i < 5; i++) { console.log("Loop iteration:", i); };
let i = 1: initialization;i < 5: condition;i++: increment;console.log(...): loop body.
This repeats until the condition becomes false.
You can also use decrement in the for loop, as shown here:
123for (let i = 15; i > 10; i--) { console.log("i =", i); }
The for loop counter is unique to its scope, so you don't need to worry about the counter name conflicting with other variables:
12345678let i = 2077; console.log("(global) i =", i); for (let i = 0; i < 4; i++) { console.log("(for) i =", i); } console.log("(global) i =", i);
Different expressions for Increment/Decrement operations can be used as well:
123for (let i = 0; i < 40; i += 7) { console.log("i =", i); };
Comparing the for and while loops
When comparing for and while loops, the for loop is often simpler and more concise. Here's an example of equivalent loops:
1234567891011// `while` let a = 1; while (a <= 3) { console.log("While:", a); a++; } // `for` for (let i = 1; i <= 3; i++) { console.log("For:", i); }
The for loop is usually shorter and keeps the counter inside its own scope.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.5
Mastering the for Loop in JavaScript
Swipe to show menu
The for loop is a common way to repeat code. It uses three parts inside the parentheses:
for (Initialization; Condition; Increment/Decrement) {
// code block
}
What each part means:
- Initialization: creates the loop counter (runs once);
- Condition: checked before every iteration;
- Increment/Decrement: updates the counter after each loop.
An iteration is one full execution of the loop body.
123for (let i = 1; i < 5; i++) { console.log("Loop iteration:", i); };
let i = 1: initialization;i < 5: condition;i++: increment;console.log(...): loop body.
This repeats until the condition becomes false.
You can also use decrement in the for loop, as shown here:
123for (let i = 15; i > 10; i--) { console.log("i =", i); }
The for loop counter is unique to its scope, so you don't need to worry about the counter name conflicting with other variables:
12345678let i = 2077; console.log("(global) i =", i); for (let i = 0; i < 4; i++) { console.log("(for) i =", i); } console.log("(global) i =", i);
Different expressions for Increment/Decrement operations can be used as well:
123for (let i = 0; i < 40; i += 7) { console.log("i =", i); };
Comparing the for and while loops
When comparing for and while loops, the for loop is often simpler and more concise. Here's an example of equivalent loops:
1234567891011// `while` let a = 1; while (a <= 3) { console.log("While:", a); a++; } // `for` for (let i = 1; i <= 3; i++) { console.log("For:", i); }
The for loop is usually shorter and keeps the counter inside its own scope.
Thanks for your feedback!