mc0de
JavaScript · Lesson 06 of 17

JavaScript Loops

Stop repeating yourself! Discover how JavaScript loops can run instructions multiple times, count through numbers, and automate repetitive tasks with just a few lines of code.

for Loops while Loops do…while Repetition
● ● ●   loops.js
for (let i = 1; i <= 5; i++) {
  console.log("Count: " + i);
}
CONSOLE OUTPUT
> Ready to count…
01 / Getting Started

What Are JavaScript Loops?

Imagine you need to display the numbers from 1 to 100 on a webpage. You could write 100 separate lines of code, but that would take a long time and be difficult to maintain.

A loop repeats a block of instructions. Instead of writing the same code again and again, you tell JavaScript how long to keep repeating it.

Without a loop
console.log(1);
console.log(2);
console.log(3);
console.log(4);
console.log(5);
With a loop
for (let i = 1; i <= 5; i++) {
  console.log(i);
}

Both examples produce the same output, but the loop is shorter and easier to change.

Think of it this way: A loop is like telling JavaScript, "Keep doing this until the task is finished."
02 / The for Loop

Repeat Code a Specific Number of Times

The for loop is useful when you know how many times you want something to happen.

Counting from 1 to 5
for (let i = 1; i <= 5; i++) {
  console.log("Number: " + i);
}
Console output
Number: 1 Number: 2 Number: 3 Number: 4 Number: 5

A for loop contains three parts:

  • let i = 1 — creates the starting counter.
  • i <= 5 — keeps the loop running while the condition is true.
  • i++ — increases the counter after each iteration.

Each time the loop runs is called an iteration.

03 / The while Loop

Repeat While a Condition Is True

The while loop checks its condition before each iteration. It's useful when the number of repetitions depends on a changing condition.

Counting with while
let count = 1;

while (count <= 5) {
  console.log(count);
  count++;
}

The loop continues until count becomes 6. At that point, the condition is false and the loop stops.

Important: Always make sure your loop has a way to stop. If count++ were missing in this example, the condition would remain true and the loop could run indefinitely.
04 / The do...while Loop

Run the Code at Least Once

A do...while loop is similar to a while loop, but it checks the condition after running the code.

Running at least once
let number = 10;

do {
  console.log(number);
  number++;
} while (number < 5);
Console output
10

Even though the condition is false, the loop runs once because JavaScript executes the do block before checking the condition.

05 / break and continue

Control What Happens Inside a Loop

Sometimes you want to stop a loop early or skip one iteration. JavaScript provides two helpful statements.

Stopping with break
for (let i = 1; i <= 10; i++) {
  if (i === 6) {
    break;
  }
  console.log(i);
}

The break statement exits the loop when i reaches 6. Only the numbers 1 through 5 are printed.

Skipping with continue
for (let i = 1; i <= 5; i++) {
  if (i === 3) {
    continue;
  }
  console.log(i);
}
Console output
1 2 4 5

The continue statement skips the rest of the current iteration and moves to the next one.

06 / Counting Backward & Skipping Numbers

Make Loops More Flexible

Loops don't have to count upward by one. You can count backward or increase the counter by larger steps.

Counting backward
for (let i = 5; i >= 1; i--) {
  console.log(i);
}
Console output
5 4 3 2 1
Counting by twos
for (let i = 2; i <= 10; i += 2) {
  console.log(i);
}
Console output
2 4 6 8 10

Changing the starting value, condition, and counter update gives you control over exactly which numbers a loop produces.

07 / Practical Loop Examples

Use Loops to Build Webpage Content

Loops can do more than print to the console. They can also create lists and display repeated information on a webpage.

Creating list items
const list = document.getElementById("numbers");

for (let i = 1; i <= 5; i++) {
  const item = document.createElement("li");
  item.textContent = "Number " + i;
  list.appendChild(item);
}

With an HTML list such as <ul id="numbers"></ul>, this code creates five list items automatically.

Good habit: Keep loops readable and avoid unnecessary work inside them. When you're learning, try small numbers first so you can understand each iteration.
🚀 mC0de Challenge

Build a Countdown & Number Generator

Create a webpage where visitors can enter a number and generate a countdown. Then challenge yourself to add a second feature that counts upward.

Your Mission

  1. Create a number input where visitors can enter a starting number.
  2. Add a button labeled Start Countdown.
  3. Use a for loop to count backward from the entered number to 1.
  4. Display each number in the live result.
  5. Show Blast Off! at the end of the countdown.
  6. Test different starting numbers to see how the loop behaves.
Extra challenge: Add a second button that generates even numbers from 2 up to the entered number. Use i += 2 to skip odd numbers.
💻 Interactive JavaScript Sandbox

Build Your Countdown Generator!

Change the loop, customize the messages, and run your code. Then enter a starting number in the live result to generate your countdown.

Live Result
Lesson 06 Recap

What You’ve Learned

You're Automating JavaScript!

You've learned how to repeat instructions efficiently and control when a loop starts, continues, or stops.

  • Use for loops to repeat code a known number of times.
  • Use while loops when repetition depends on a condition.
  • Use do...while when the code must run at least once.
  • Control loops with break and continue.
  • Count backward or skip numbers using different counter updates.
  • Create repeated webpage content with JavaScript.

Next up: Functions — learn how to organize reusable instructions and build smarter programs!

Your Learning Journey

0 / 17 Completed

Keep going! Every completed lesson brings you closer to mastering JavaScript.

🎉

Nice Work!

You've learned how to repeat instructions using for, while, and do...while loops, control iterations with break and continue, and generate numbers automatically. You've even built your own interactive countdown generator!