JavaScript for loop
The for
loop in JavaScript is one of the most commonly used control structures for iterating over a block of code a specific number of times. It is particularly useful when the number of iterations is known beforehand.
Syntax
The basic syntax of a for
loop is:
for (initialization; condition; increment) {
// Code to execute on each iteration
}
Components
- Initialization: This is where you initialize the loop counter or variable. It is executed only once, at the beginning of the loop.
- Condition: This is a boolean expression that is evaluated before each iteration. The loop continues to execute as long as this condition evaluates to
true
. When the condition becomesfalse
, the loop stops. - Increment: This expression is executed after each iteration of the loop. It is typically used to update the loop counter.
Example
Here’s a simple example of a for
loop that prints numbers from 0 to 4:
for (let i = 0; i < 5; i++) {
console.log(i);
}
- Initialization:
let i = 0
sets up the loop counteri
to 0. - Condition:
i < 5
checks ifi
is less than 5. If true, the loop continues; otherwise, it stops. - Increment:
i++
increases the value ofi
by 1 after each iteration.
Detailed Breakdown
Initialization:
let i = 0;
- Initializes the loop counter
i
to 0.
- Initializes the loop counter
Condition:
i < 5
- Before each iteration, it checks if
i
is less than 5. If true, the loop executes; if false, it terminates.
- Before each iteration, it checks if
Body:
console.log(i);
- Executes the code block inside the loop on each iteration.
Increment:
i++
- Increases the value of
i
by 1 after each iteration.
- Increases the value of
Use Cases
Iterating Over Arrays: Often used to loop through the elements of an array.
let fruits = ['apple', 'banana', 'cherry']; for (let i = 0; i < fruits.length; i++) { console.log(fruits[i]); }
- Explanation: This loops through the
fruits
array and logs each element.
- Explanation: This loops through the
Executing Code a Fixed Number of Times: When you need to run code a certain number of times.
for (let i = 1; i <= 10; i++) { console.log('Iteration ' + i); }
- Explanation: This prints "Iteration 1" through "Iteration 10".
Variations
Looping with Different Step Values:
for (let i = 0; i <= 10; i += 2) { console.log(i); }
- Explanation: This prints even numbers from 0 to 10 (0, 2, 4, 6, 8, 10).
Infinite Loop (Requires careful use to avoid issues):
for (let i = 0; ; i++) { if (i > 5) break; console.log(i); }
- Explanation: This loop continues indefinitely until
i
is greater than 5, at which point it breaks.
- Explanation: This loop continues indefinitely until
Nested Loops:
for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { console.log(`i = ${i}, j = ${j}`); } }
- Explanation: This example uses a
for
loop inside anotherfor
loop to print combinations ofi
andj
values.
- Explanation: This example uses a
Summary
- Initialization: Sets up the loop counter.
- Condition: Determines whether the loop should continue.
- Increment: Updates the loop counter after each iteration.
- Flexibility: Can be used for iterating a fixed number of times or over arrays and other collections.