JavaScript Loops
Loops in JavaScript are used to execute a block of code repeatedly based on a specified condition. They are essential for tasks that require repetitive operations, such as iterating over arrays, processing data, or performing repetitive calculations. JavaScript supports several types of loops:
1. for
Loop
The for
loop is used to execute a block of code a specific number of times. It is commonly used when the number of iterations is known beforehand.
Syntax:
for (initialization; condition; increment) {
// Code to execute on each iteration
}
Example:
for (let i = 0; i < 5; i++) {
console.log(i);
}
- Explanation:
initialization
sets up the loop counter (let i = 0
).condition
(i < 5
) is evaluated before each iteration. If true, the loop continues; if false, it stops.increment
(i++
) updates the counter after each iteration.
2. while
Loop
The while
loop executes a block of code as long as a specified condition evaluates to true
. It is useful when the number of iterations is not known beforehand.
Syntax:
while (condition) {
// Code to execute while condition is true
}
Example:
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
- Explanation:
- The loop continues as long as
i < 5
. - The counter
i
is incremented inside the loop to avoid an infinite loop.
- The loop continues as long as
3. do...while
Loop
The do...while
loop is similar to the while
loop but guarantees that the code block is executed at least once because the condition is evaluated after the code block.
Syntax:
do {
// Code to execute
} while (condition);
Example:
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
- Explanation:
- The loop executes the code block first and then checks if
i < 5
to determine if it should continue.
- The loop executes the code block first and then checks if
4. for...in
Loop
The for...in
loop iterates over the enumerable properties of an object.
Syntax:
for (let key in object) {
// Code to execute for each property
}
Example:
let person = { name: 'Alice', age: 25, city: 'New York' };
for (let key in person) {
console.log(key + ': ' + person[key]);
}
- Explanation:
- The loop iterates over each property (
key
) in theperson
object and logs the property name and value.
- The loop iterates over each property (
5. for...of
Loop
The for...of
loop iterates over the values of iterable objects (like arrays, strings, maps, and sets).
Syntax:
for (let value of iterable) {
// Code to execute for each value
}
Example:
let numbers = [1, 2, 3, 4, 5];
for (let number of numbers) {
console.log(number);
}
- Explanation:
- The loop iterates over each value in the
numbers
array and logs it.
- The loop iterates over each value in the
6. Breaking Out of Loops
You can use the break
statement to exit a loop prematurely based on a condition.
Example:
for (let i = 0; i < 10; i++) {
if (i === 5) {
break;
}
console.log(i);
}
- Explanation:
- The loop exits when
i
equals 5, so only numbers 0 through 4 are logged.
- The loop exits when
7. Skipping Iterations
You can use the continue
statement to skip the current iteration and proceed to the next one.
Example:
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) {
continue;
}
console.log(i);
}
- Explanation:
- The
continue
statement skips logging the even numbers, so only odd numbers are logged.
- The
Summary
for
Loop: Ideal when the number of iterations is known.while
Loop: Useful when the number of iterations is not known and the loop should run while a condition is true.do...while
Loop: Similar towhile
, but guarantees execution of the block at least once.for...in
Loop: Iterates over the properties of an object.for...of
Loop: Iterates over the values of iterable objects.break
: Exits the loop early.continue
: Skips the current iteration and continues with the next one.