Lesson 6 - Loops
Loops in JavaScript
For Loop
For Loop is used to repeat a block of code multiple times. It can count with numbers, or go through a sequence (like strings or arrays) one by one.
for (let letter of "HELLO") {
console.log(letter);
}
for (let i = 1; i <= 5; i++) {
console.log(i);
}
let fruits = ["apple", "banana", "cherry"];
for (let f of fruits) {
console.log(f);
}
For Loops with Conditions
You can combine for loops with if statements to make them more powerful.
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) {
console.log(i);
}
}
for (let i = 1; i <= 5; i++) {
console.log(`2 x ${i} = ${2 * i}`);
}
for (let x = 0; x < 3; x++) {
for (let y = 0; y < 3; y++) {
console.log(x, y);
}
}
Tip: A for loop has 3 parts inside the brackets:
start; condition; step. The loop keeps going while the condition is true.
Change the step to count differently.
for (let x = 0; x < 3; x++) {
console.log(x);
}
for (let y = 0; y < 10; y += 3) {
console.log(y);
}
While Loop
While Loop is used when you want to repeat a block of code as long as a condition is true.
let i = 1;
while (i <= 4) {
console.log(i);
i = i + 1;
}
1
2
3
4
⚠️ Be careful! If the condition is always true, the loop will run forever.
Always make sure the loop updates the variable so it can eventually stop.
Break Statement exits the loop early.
while (i <= 10) {
if (i === 5) {
break;
}
console.log(i);
i = i + 1;
}
Continue Statement skips the current loop step and goes to the next one.
while (i < 5) {
i = i + 1;
if (i === 3) {
continue;
}
console.log(i);
}
Print numbers from 1 to 5
console.log(i);
}
💡 Change the loop to print 1–10.
Print each letter of a word
for (let letter of word) {
console.log(letter);
}
💡 Try changing the word.
Print items in a list
for (let f of fruits) {
console.log(f);
}
💡 Add another fruit and run again.
Print a multiplication table for 2
console.log(`2 x ${i} = ${2 * i}`);
}
💡 Try table of 5.
Add all numbers 1 to 10
for (let i = 1; i <= 10; i++) {
total = total + i;
}
console.log(total);
💡 Change to add numbers 1–20.
Print only even numbers between 1 and 20
if (i % 2 === 0) {
console.log(i);
}
}
💡 Can you make it print odd numbers instead?
Sum numbers until 0 is entered
let num = Number(prompt("Enter a number (0 to stop): "));
while (num !== 0) {
total = total + num;
num = Number(prompt("Enter a number (0 to stop): "));
}
console.log(`Total: ${total}`);
👉 Try different numbers.
Print a square of stars ⭐
console.log("*****");
}
💡 Change it to make a rectangle.
Try nested loop
let starRow = "";
for (let x = 0; x < 4; x++) {
starRow += "*";
}
console.log(starRow);
}
Cooking recipe
let ingredients = ["flour", "sugar", "eggs", "milk", "butter"];
console.log("Let's make muffins!");
console.log("We need:");
for (let item of ingredients) {
console.log("- " + item);
}
console.log("Mix everything together and bake! 🎂");
💡 Try your favorite food recipe
Guess the secret number
let guess = 0;
while (guess !== secret) {
guess = Number(prompt("Guess the number: "));
}
console.log("Correct! 🎉");
👉 Change the secret number.
Use break to stop the loop
while (i <= 10) {
if (i === 5) {
break;
}
console.log(i);
i = i + 1;
}
👉 What numbers will print?
Use continue to skip a number
while (i < 5) {
i = i + 1;
if (i === 3) {
continue;
}
console.log(i);
}
👉 Which number is skipped?