«Back to Home

Learn JavaScript

Topics

Loops in JavaScript (for, while, do…while)

Loops allow you to repeat a block of code multiple times without repeating the code. They are extremely useful in programming because many tasks involve repetition—printing numbers, processing lists, checking conditions repeatedly, etc.

For college students and freshers, loops are an essential topic because they appear in almost every program, assignment, and interview question.

In this chapter, you will learn:

  • for loop

  • while loop

  • do…while loop

  • When to use which loop

  • Common loop mistakes

Let’s start.

Why Do We Need Loops?

Imagine you want to print numbers from 1 to 100.

Without loops:

console.log(1);
console.log(2);
console.log(3);
// ... up to 100 (not practical)

With a loop:

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

Much simpler!

Loops save time, reduce mistakes, and make your code cleaner.

1. for Loop

The for loop is the most commonly used loop in JavaScript.

Syntax:

for (initialization; condition; increment) {
    // code to run
}

Example 1: Print numbers from 1 to 5

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

Output:

1
2
3
4
5

Example 2: Print even numbers from 2 to 10

for (let num = 2; num <= 10; num += 2) {
    console.log(num);
}

Output:

2
4
6
8
10

2. while Loop

The while loop runs as long as the condition is true.

Syntax:

while (condition) {
    // code
}

Example: Print numbers from 1 to 5

let i = 1;

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

Output:

1
2
3
4
5

Use while when you don’t know how many times the loop will run.

3. do…while Loop

The do…while loop runs the code at least once, even if the condition is false.

Syntax:

do {
    // code
} while (condition);

Example:

let i = 1;

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

Output:

1
2
3
4
5

Even if the condition is false initially, do…while will run once.

Example Where do…while is Useful

If you want to ask a user for input at least once.

let num = 1;

do {
    console.log("Number is:", num);
    num++;
} while (num <= 3);

Output:

Number is: 1
Number is: 2
Number is: 3

Which Loop Should You Use?

SituationBest Loop
You know the exact number of repetitionsfor
You don’t know how many times the loop should runwhile
You want the code to run at least oncedo…while

Common Mistakes Beginners Make

1. Forgetting to increment the loop variable

let i = 1;
while (i <= 5) {
    console.log(i);
    // forgot i++
}

This causes an infinite loop.

2. Wrong condition

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

Condition is false at the start ? loop never runs.

3. Writing complex logic inside loops

Try to keep the loop clean and simple.

Real-Life Example: Sum of First 5 Numbers

let sum = 0;

for (let i = 1; i <= 5; i++) {
    sum = sum + i;
}

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

Output:

Sum: 15

Real-Life Example: Print Characters of a String

let name = "Rohan";

for (let i = 0; i < name.length; i++) {
    console.log(name[i]);
}

Output:

R
o
h
a
n

This demonstrates how loops facilitate string processing.

Practice Tasks (Do It Yourself)

  1. Print numbers 1 to 50.

  2. Print the table of 5.

  3. Print all odd numbers between 1 and 20.

  4. Find the sum of numbers from 1 to 100.

  5. Print each character of your name using a loop.

Author
Ranjit Powar
0 6.3k 1.5m