JavaScript Loops Practice Questions with Solutions

Introductions

Loops allow JavaScript to repeat a block of code multiple times without writing the same code again and again. They are useful for displaying lists, calculating totals, processing numbers, and working with arrays. In this chapter, you will practice for, while, and do...while loops with simple examples. The questions gradually move from basic repetition to practical programming problems. JavaScript Loops Practice Questions with Solutions help to understand the concepts.

Question 1: Print Numbers from 1 to 10

Problem

Write a JavaScript program using a for loop to print numbers from 1 to 10.

Solution

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

Output

1
2
3
4
5
6
7
8
9
10

Step-by-step Explanation

  1. let i = 1 starts the loop with i equal to 1.
  2. i <= 10 checks whether the loop should continue.
  3. console.log(i) prints the current value.
  4. i++ increases i by 1.
  5. The loop continues until i becomes 11.
  6. When i <= 10 becomes false, the loop stops.

Question 2: Print Even Numbers from 1 to 20

Problem

Use a for loop to print all even numbers between 1 and 20.

Solution

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

Output

2
4
6
8
10
12
14
16
18
20

Step-by-step Explanation

  1. The loop starts from 1.
  2. It continues until 20.
  3. i % 2 finds the remainder after dividing i by 2.
  4. If the remainder is 0, the number is even.
  5. Only even numbers are displayed.

Question 3: Calculate the Sum of Numbers from 1 to 10

Problem

Use a loop to calculate the sum of all numbers from 1 to 10.

Solution

let sum = 0;

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

console.log(sum);

Output

55

Step-by-step Explanation

  1. sum starts with 0.
  2. The loop starts with i = 1.
  3. Each value of i is added to sum.
  4. The calculation continues until i reaches 10.
  5. The final calculation is:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
  1. The final value of sum is 55.

Question 4: Print a Multiplication Table

Problem

Print the multiplication table of 5 from 1 to 10.

Solution

let number = 5;

for (let i = 1; i <= 10; i++) {
    console.log(number * i);
}

Output

5
10
15
20
25
30
35
40
45
50

Step-by-step Explanation

  1. The variable number contains 5.
  2. The loop starts with i = 1.
  3. number * i calculates the table value.
  4. The loop increases i by 1 after each iteration.
  5. The loop stops after i = 10.

For a more readable table, you can write:

let number = 5;

for (let i = 1; i <= 10; i++) {
    console.log(number + " x " + i + " = " + (number * i));
}

Output:

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Question 5: Print Numbers in Reverse

Problem

Use a for loop to print numbers from 10 down to 1.

Solution

for (let i = 10; i >= 1; i--) {
    console.log(i);
}

Output

10
9
8
7
6
5
4
3
2
1

Step-by-step Explanation

  1. The loop starts with i = 10.
  2. The condition checks whether i >= 1.
  3. The current value is printed.
  4. i-- decreases the value by 1.
  5. The loop continues until i becomes 0.
  6. Since 0 >= 1 is false, the loop stops.

Question 6: Calculate the Factorial of a Number

Problem

Calculate the factorial of 5 using a loop.

The calculation is:

5 × 4 × 3 × 2 × 1 = 120

Solution

let number = 5;
let factorial = 1;

for (let i = 1; i <= number; i++) {
    factorial = factorial * i;
}

console.log(factorial);

Output

120

Step-by-step Explanation

  1. number contains 5.
  2. factorial starts at 1.
  3. The loop starts from 1.
  4. Each number is multiplied into factorial.
  5. The calculations are:
1 × 1 = 1
1 × 2 = 2
2 × 3 = 6
6 × 4 = 24
24 × 5 = 120
  1. The final factorial is 120.

Question 7: Use a While Loop

Problem

Use a while loop to print numbers from 1 to 5.

Solution

let i = 1;

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

    i++;
}

Output

1
2
3
4
5

Step-by-step Explanation

  1. i starts at 1.
  2. The while loop checks i <= 5.
  3. If the condition is true, the number is displayed.
  4. i++ increases the value by 1.
  5. The process repeats.
  6. When i becomes 6, the condition becomes false.
  7. The loop stops.

A while loop is useful when the number of repetitions depends on a condition.


Question 8: Use a Do…While Loop

Problem

Use a do...while loop to print numbers from 1 to 5.

Solution

let i = 1;

do {
    console.log(i);

    i++;
} while (i <= 5);

Output

1
2
3
4
5

Step-by-step Explanation

  1. i starts at 1.
  2. The do block executes first.
  3. The current value is printed.
  4. i++ increases the value.
  5. The while condition is checked.
  6. The process continues while i <= 5.
  7. The loop stops when the condition becomes false.

The important feature of do...while is that the code inside do executes at least once.


Question 9: Stop a Loop Using break

Problem

Print numbers from 1 to 10, but stop the loop when the number reaches 6.

Solution

for (let i = 1; i <= 10; i++) {
    if (i === 6) {
        break;
    }

    console.log(i);
}

Output

1
2
3
4
5

Step-by-step Explanation

  1. The loop starts at 1.
  2. Numbers are printed normally.
  3. When i becomes 6, the if condition becomes true.
  4. The break statement immediately stops the loop.
  5. Therefore, numbers after 5 are not printed.

break is used when you want to completely stop a loop.


Question 10: Skip a Number Using continue

Problem

Print numbers from 1 to 10, but skip the number 5.

Solution

for (let i = 1; i <= 10; i++) {
    if (i === 5) {
        continue;
    }

    console.log(i);
}

Output

1
2
3
4
6
7
8
9
10

Step-by-step Explanation

  1. The loop starts from 1.
  2. Each number is checked.
  3. When i becomes 5, the condition becomes true.
  4. continue skips the current iteration.
  5. The number 5 is not printed.
  6. The loop then continues with 6.
  7. The remaining numbers are printed normally.

continue skips the current iteration but does not stop the entire loop.

Key Takeaways

  • Loops repeat a block of code multiple times.
  • A for loop is useful when you know how many times something should repeat.
  • A while loop continues while a condition remains true.
  • A do...while loop executes its code at least once before checking the condition.
  • i++ increases a value by 1.
  • i-- decreases a value by 1.
  • Loops are useful for calculations, tables, counting, and processing data.
  • break completely stops a loop.
  • continue skips the current iteration.
  • The loop condition must eventually become false to prevent an infinite loop.

FAQs

1. What is a loop in JavaScript?

A loop is a programming structure that repeats a block of code while a specified condition is satisfied.

2. What is a for loop?

A for loop is commonly used when you know the starting value, ending condition, and how the counter should change.

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

3. What is a while loop?

A while loop repeatedly executes code as long as its condition is true.

let i = 1;

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

4. What is the difference between while and do…while?

A while loop checks its condition before executing the code. A do...while loop executes the code first and checks the condition afterward.

5. What does break do in a loop?

break immediately stops the entire loop.

for (let i = 1; i <= 10; i++) {
    if (i === 5) {
        break;
    }

    console.log(i);
}

6. What does continue do in JavaScript?

continue skips the current iteration and moves to the next iteration of the loop.

7. How can I create an infinite loop?

An infinite loop happens when the loop condition never becomes false.

For example:

while (true) {
    console.log("Running");
}

This loop has no stopping condition and should generally be avoided unless you intentionally manage how it exits.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top