Today you will learn:
for loopwhile loopBy the end, you will be able to automate repetitive tasks in JavaScript.
Loops are used to repeat code multiple times.
Example:
The for loop runs a specific number of times.
for (let i = 1; i <= 5; i++) {
console.log(i);
}
Explanation:
i = 1 → starti <= 5 → conditioni++ → increaseThe while loop runs as long as a condition is true.
let i = 1;
while (i <= 5) {
console.log(i);
i++;
}
for (let i = 1; i <= 5; i++) {
console.log(i);
}
Complete the following tasks:
Example:
// 1 to 10
for (let i = 1; i <= 10; i++) {
console.log(i);
}
// Even numbers
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) {
console.log(i);
}
}