A Beginner’s Guide to JavaScript Loops with Real-Life Examples

JavaScript loops 2025 – JavaScript loops help us repeat actions without writing the same code multiple times. Imagine if you had to remind your friend to drink water every hour manually.

JavaScript loops 2025

Instead of sending messages every hour yourself, wouldn’t it be great if there was a way to automate this? That’s what loops do in programming! Loops make our code efficient and save us from repetitive work.

What is a Loop?

A loop is a way to repeat a task multiple times until a specific condition is met.

Think of it like setting an alarm every morning. The alarm rings daily until you turn it off. Similarly, a loop runs code repeatedly until the condition tells it to stop.

JavaScript has several types of loops:

  1. for loop – Best when you know how many times to repeat.
  2. while loop – Runs until a condition becomes false.
  3. do…while loop – Similar to while, but runs at least once.
  4. for…of loop – Loops through arrays and strings.
  5. for…in loop – Loops through object properties.

Let’s explore each one in detail.

1. The for Loop

The for loop is used when we know exactly how many times we want to repeat something.

Structure of a for loop:

for (initialization; condition; increment/decrement) {
  // Code to be repeated
}
  • Initialization: Starts the loop with a value.
  • Condition: The loop runs while this is true.
  • Increment/Decrement: Increases or decreases the value.

Real-Life Example: Counting Apples

Imagine you have 5 apples, and you want to count them one by one.

for (let i = 1; i <= 5; i++) {
  console.log("Apple number:", i);
}

What happens here?

  • We start counting from 1.
  • The loop runs while i is ≤ 5.
  • Each time, i increases by 1.

Using for Loop with an Array

If we have an array of fruits:

let fruits = ["Apple", "Banana", "Mango", "Orange"];

for (let i = 0; i < fruits.length; i++) {
  console.log("I love", fruits[i]);
}

Output:

I love Apple  
I love Banana  
I love Mango  
I love Orange  

2. The while Loop

The while loop is useful when we don’t know how many times something needs to repeat. It runs as long as the condition remains true.

Structure of a while loop:

while (condition) {
  // Code to be repeated
}

Real-Life Example: Drinking Water

Let’s say you want to drink water every hour until you’ve had 5 glasses.

let glasses = 0;

while (glasses < 5) {
  glasses++;
  console.log("Drank glass number:", glasses);
}

What happens here?

  • The loop starts with glasses = 0.
  • It keeps running until glasses reaches 5.

3. The do...while Loop

This is similar to the while loop but with one difference:
It runs at least once, even if the condition is false at the start.

Structure of a do...while loop:

do {
  // Code to be repeated
} while (condition);

Real-Life Example: Asking for Ice Cream

Imagine a child asking for ice cream at least once, even if the answer is “No”.

let asked = 0;

do {
  asked++;
  console.log("Can I have ice cream?");
} while (asked < 1);

What happens here?

  • The child asks at least once before checking the condition.

4. The for...of Loop

This loop is perfect for looping through arrays and strings. It directly gives you the values, so you don’t need an index.

Structure of a for...of loop:

for (let item of collection) {
  // Code to be repeated
}

Real-Life Example: Reading Books

Let’s say you have a list of books and want to read each one.

let books = ["Harry Potter", "Lord of the Rings", "Percy Jackson"];

for (let book of books) {
  console.log("Reading:", book);
}

Output:

Reading: Harry Potter  
Reading: Lord of the Rings  
Reading: Percy Jackson  

What happens here?

  • Each book is read one by one without needing an index.

5. The for...in Loop

This loop is used for looping through object properties.

Structure of a for...in loop:

for (let key in object) {
  // Code to be repeated
}

Real-Life Example: A Student’s Report Card

Imagine you have an object with subjects and marks.

let reportCard = {
  Math: 95,
  Science: 90,
  English: 85
};

for (let subject in reportCard) {
  console.log(subject, ":", reportCard[subject]);
}

Output:

Math : 95  
Science : 90  
English : 85  

What happens here?

  • We loop through each subject and print its marks.
 types of loops

Break and Continue Statements

break: Stop the loop immediately

Example: Stop counting at 3.

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

Output:

1  
2  

continue: Skip the current iteration and move to the next

Example: Skip number 3.

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

Output:

1  
2  
4  
5  

JavaScript Reverse for Loop

A reverse loop starts from a higher number and decreases.

Example: Counting Down from 5

javascriptCopy codefor (let i = 5; i >= 1; i--) {
console.log("Countdown:", i);
}

Forever Loop in JavaScript (Endless Loop)

An infinite loop runs forever unless stopped manually. Be careful with these!

Example: Endless Loop

javascriptCopy codewhile (true) {
  console.log("This loop runs forever!");
}

To stop this loop, you have to manually break it.

Array Sum with Nested Loops in JavaScript

A nested loop is a loop inside another loop.

Example: Summing a 2D Array

javascriptCopy codelet numbers = [
  [1, 2, 3],
  [4, 5, 6]
];

let sum = 0;

for (let row of numbers) {
  for (let num of row) {
    sum += num;
  }
}

console.log("Total sum:", sum);

JavaScript Endless Loop (Another Way)

A for loop without an end condition runs forever.

javascriptCopy codefor (;;) {
  console.log("This will never stop!");
}

Always add a break condition to avoid freezing your system.

JavaScript

Enhanced for Loop in JavaScript

JavaScript doesn’t have a special enhanced for loop, but for...of works similarly.

Example: Looping Through an Array

javascriptCopy codelet colors = ["Red", "Green", "Blue"];

for (let color of colors) {
console.log("Color:", color);
}

JavaScript Loop Through JSON Array

Example: Extracting Data from JSON

javascriptCopy codelet users = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 }
];

for (let user of users) {
  console.log(user.name, "is", user.age, "years old.");
}

Loops That Start with a Letter in JavaScript

You can filter an array and loop only through items that start with a specific letter.

Example: Finding Names That Start with “A”

javascriptCopy codelet names = ["Alice", "Bob", "Anna", "Charlie"];

for (let name of names) {
if (name.startsWith("A")) {
console.log("Name that starts with A:", name);
}
}

JavaScript loops 2025 FAQs

What is the difference between for and while loops?

for is used when we know the number of iterations.
while runs until a condition is false.

What happens if I forget the break condition in an infinite loop?

Your browser or program might crash. Always be careful!

Can I loop through an object in JavaScript?

Yes! Use for...in to loop through object properties.

What is the best way to loop through an array?

Use for...of or a regular for loop.

Can I have a loop inside another loop?

Yes, it’s called a nested loop. It’s useful for multidimensional arrays.

Summary

Loops save time and make our code efficient by automating repetitive tasks.

Key Takeaways:

  • Use for when you know the number of iterations.
  • Use while when you don’t know the exact count.
  • Use do...while when you need at least one execution.
  • Use for...of for arrays and strings.
  • Use for...in for objects.
  • Use reverse for to count backward.
  • Be careful with infinite loops (they can crash programs!).

JavaScript DOM (Document Object Model) – A Complete Guide

Ajay

Ajay is a passionate tech enthusiast and digital creator, dedicated to making complex technology easy to understand. With years of experience in software, gadgets, and web development, Ajay shares tutorials, reviews, and tips to help you stay ahead in the digital world.

Leave a Comment