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
let i = 1starts the loop withiequal to1.i <= 10checks whether the loop should continue.console.log(i)prints the current value.i++increasesiby1.- The loop continues until
ibecomes11. - When
i <= 10becomes 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
- The loop starts from
1. - It continues until
20. i % 2finds the remainder after dividingiby2.- If the remainder is
0, the number is even. - 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
sumstarts with0.- The loop starts with
i = 1. - Each value of
iis added tosum. - The calculation continues until
ireaches10. - The final calculation is:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
- The final value of
sumis55.
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
- The variable
numbercontains5. - The loop starts with
i = 1. number * icalculates the table value.- The loop increases
iby1after each iteration. - 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
- The loop starts with
i = 10. - The condition checks whether
i >= 1. - The current value is printed.
i--decreases the value by1.- The loop continues until
ibecomes0. - Since
0 >= 1is 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
numbercontains5.factorialstarts at1.- The loop starts from
1. - Each number is multiplied into
factorial. - The calculations are:
1 × 1 = 1
1 × 2 = 2
2 × 3 = 6
6 × 4 = 24
24 × 5 = 120
- 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
istarts at1.- The
whileloop checksi <= 5. - If the condition is true, the number is displayed.
i++increases the value by1.- The process repeats.
- When
ibecomes6, the condition becomes false. - 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
istarts at1.- The
doblock executes first. - The current value is printed.
i++increases the value.- The
whilecondition is checked. - The process continues while
i <= 5. - 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
- The loop starts at
1. - Numbers are printed normally.
- When
ibecomes6, theifcondition becomes true. - The
breakstatement immediately stops the loop. - Therefore, numbers after
5are 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
- The loop starts from
1. - Each number is checked.
- When
ibecomes5, the condition becomes true. continueskips the current iteration.- The number
5is not printed. - The loop then continues with
6. - 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
forloop is useful when you know how many times something should repeat. - A
whileloop continues while a condition remains true. - A
do...whileloop executes its code at least once before checking the condition. i++increases a value by1.i--decreases a value by1.- Loops are useful for calculations, tables, counting, and processing data.
breakcompletely stops a loop.continueskips 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.
