6.1 while Loop
Loops in JavaScript let you run a block of code repeatedly as long as a certain condition holds true. This is a crucial part of programming that helps automate repetitive tasks.
The while
loop executes a block of code as long as the condition is true (true
).
Syntax:
while (condition) {
// code that runs while the condition is true
}
Example:
let i = 0;
while (i < 5) {
console.log(`Iteration number ${i}`);
i++;
}
// Output:
// Iteration number 0
// Iteration number 1
// Iteration number 2
// Iteration number 3
// Iteration number 4
In this example:
- The loop continues as long as the value of
i
is less than 5 - Inside the loop, the value of
i
is incremented by 1 after each iteration
6.2 Executing a block of code n times
Quite often you need to execute a block of code a fixed number of times. This can be written in a while
loop:
let i = 0;
while (i < 5) {
console.log(`Iteration number ${i}`);
i++;
}
In general, it will look like this:
initialization
while (condition) {
block of code
step
}
6.3 for Loop
for Loop
The for
loop is one of the most commonly used loops in JavaScript. It's specifically designed to repeat a block of code a certain number of times.
Syntax:
for (initialization; condition; step) {
// code to be executed on each iteration
}
Example:
for (let i = 0; i < 5; i++) {
console.log(`Iteration number ${i}`);
}
// Output:
// Iteration number 0
// Iteration number 1
// Iteration number 2
// Iteration number 3
// Iteration number 4
Explanation:
let i = 0
— initializes the variablei
, which will be the iteration counteri < 5
— the condition under which the loop continues. The loop stops wheni
equals 5i++
— the step, which is executed after each iteration, incrementing the value ofi
by 1
6.4 do...while Loop
The do...while
loop is similar to the while
loop, but with the difference that the block of code is executed at least once before the condition is tested.
Syntax:
do {
// code to be executed at least once
} while (condition);
Example:
let i = 0;
do {
console.log(`Iteration number ${i}`);
i++;
} while (i < 5);
// Output:
// Iteration number 0
// Iteration number 1
// Iteration number 2
// Iteration number 3
// Iteration number 4
In this example:
- The block of code inside
do
will execute at least once, even if the condition is initially false - After executing the block of code, the condition is tested, and if true, the loop continues
6.5 Comparing Loops
Loop | When to use | Features |
---|---|---|
for | When the number of iterations is known | Compact syntax, convenient for arrays |
while | When the number of iterations is not known in advance | Condition is tested before each iteration |
do-while | When you want to execute a block of code at least once | Condition is tested after executing the block of code |
Examples of using loops
Looping through an array with for:
let array = [1, 2, 3, 4, 5];
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
// Output: 1 2 3 4 5
Using while to wait for a condition:
let n = 0;
let x = 0;
while (n < 3) {
n++;
x += n;
}
console.log(x); // 6 (1 + 2 + 3)
Using do-while to execute at least one iteration:
let i = 0;
do {
console.log(i);
i++;
} while (i < 0);
// Output: 0 (loop executed once despite the false condition)
GO TO FULL VERSION