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.

// Print each letter
for (let letter of "HELLO") {
 console.log(letter);
}
// Print numbers 1 to 5
for (let i = 1; i <= 5; i++) {
 console.log(i);
}
// Loop through array
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.

// Print only even numbers
for (let i = 1; i <= 10; i++) {
  if (i % 2 === 0) {
    console.log(i);
  }
}
// Multiplication table of 2
for (let i = 1; i <= 5; i++) {
  console.log(`2 x ${i} = ${2 * i}`);
}
// Nested loop
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.

// Result will be: 0 1 2
for (let x = 0; x < 3; x++) {
 console.log(x);
}
// Result will be: 0 3 6 9
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.

// Example: count from 1 to 4
let i = 1;
while (i <= 4) {
  console.log(i);
  i = i + 1;
}
// Output
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.

let i = 1;
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.

let i = 0;
while (i < 5) {
 i = i + 1;
 if (i === 3) {
  continue;
 }
 console.log(i);
}

Print numbers from 1 to 5

for (let i = 1; i <= 5; i++) {
  console.log(i);
}

💡 Change the loop to print 1–10.


Print each letter of a word

let word = "HELLO";
for (let letter of word) {
  console.log(letter);
}

💡 Try changing the word.


Print items in a list

let fruits = ["apple", "banana", "cherry"];
for (let f of fruits) {
  console.log(f);
}

💡 Add another fruit and run again.


Print a multiplication table for 2

for (let i = 1; i <= 10; i++) {
  console.log(`2 x ${i} = ${2 * i}`);
}

💡 Try table of 5.


Add all numbers 1 to 10

let total = 0;
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

for (let i = 1; i <= 20; i++) {
  if (i % 2 === 0) {
    console.log(i);
  }
}

💡 Can you make it print odd numbers instead?


Sum numbers until 0 is entered

let total = 0;
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 ⭐

for (let i = 0; i < 5; i++) {
  console.log("*****");
}

💡 Change it to make a rectangle.

Try nested loop

for (let i = 0; i < 5; i++) {
  let starRow = "";
  for (let x = 0; x < 4; x++) {
    starRow += "*";
  }
  console.log(starRow);
}

Cooking recipe

// Muffin Recipe using an array
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 secret = 7;
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

let i = 1;
while (i <= 10) {
  if (i === 5) {
    break;
  }
  console.log(i);
  i = i + 1;
}

👉 What numbers will print?


Use continue to skip a number

let i = 0;
while (i < 5) {
  i = i + 1;
  if (i === 3) {
    continue;
  }
  console.log(i);
}

👉 Which number is skipped?

Back Next Lesson