Published on

Loops in JavaScript

As a beginner in JavaScript, you might have come across the need to repeat certain actions multiple times. This is where loops come in handy. There are three main types of loops in JavaScript:

  • while loop
  • for loop
  • do-while loop

while loop

Think about taking an exam where the teacher is grading your paper right in front of you (just for fun). Initially, you feel nervous as your score starts from zero. Imagine your marks would increase by 10 for each attempt, and eventually you'll feel a sense of relief once your marks reach 50, which is the passing percentage.

let score = 0;
while (score < 50) {
  score = score + 10;
  console.log("Score = " + score);
  console.log("Teacher: You need to work towards passing percentage of 50");
}

console.log("Score = " + score);
console.log("Sense of relief!");

This is an example of how a while loop works. It can be used to repeat a set of actions until a certain condition is met.

The code uses a while loop to increment the score by 10 in each iteration until it reaches the passing percentage of 50. In each iteration, the code logs the current score and a message to motivate the student to work towards the passing percentage. Once the score reaches 50, the code logs the final score and a message to indicate the sense of relief.

do-while loop

Now, let's consider the same exam scenario, but this time, let's make use of the do-while loop. With a do-while loop, the code block will run at least once, even if the condition is not met.

In our exam scenario, we'll first attempt an answer, and then check if it's correct. If it's correct, we'll add 10 to our score. We'll keep repeating this process until we reach the passing percentage of 50.

Here's the code for this scenario:

let score = 0;

do {
  score = score + 10;
  console.log("Score = " + score);
  console.log("Teacher: You need to work harder!");
} while (score < 50);

console.log("Score = " + score);
console.log("Sense of relief!");

In this code, the code block inside the do-while loop runs at least once and continues to run until the score is greater than or equal to 50. The code block inside the loop only runs when the condition is met, and once it is, the loop stops.

for loop

for loop works in a similar manner, with slight variations. Here's a code example using a for loop to print "Hello World" 10 times:

for (let i = 0; i < 10; i++) {
  console.log("Attempt: " + i);    
  console.log("Hello World");
}

In the above code, the for loop starts with let i = 0, which is the starting value of the counter variable i. The condition i < 10 states that the loop should continue as long as i is less than 10. Finally, i++ increases the value of i by 1 after each iteration of the loop.

Conclusion

for loop, while loop, and do-while loop are the three main types of loops in JavaScript. Each of these loops has its own syntax and serves a different purpose. Understanding these loops is crucial in making an informed decision on which one to use in your code, based on your specific requirements. By mastering these loops, you will be able to write efficient and effective code that can handle repetitive tasks with ease.