“Loops Unraveled: Exploring the Different Types of Loops with a Fun Story!”

Ashwini Paraye
5 min readMay 2, 2023

--

In the world of programming, loops are a powerful tool that can automate repetitive tasks and save developers time and effort. For those new to programming, understanding loops can be a bit daunting. But fear not! In this article, we will use a fun story to explain loops in JavaScript and why they are so important.

Meet Jack, a young JavaScript programmer who was tasked with printing the numbers from 1 to 10 on the screen. At first, he wrote the code to print out “1” and then copied and pasted the code 9 more times, changing the number each time. It worked, but he knew that this wasn’t the best way to write his program.

That’s when Jack remembered the power of loops. He knew that loops were a way to automate repetitive tasks, and he decided to give them a try.

Jack started with a “for” loop, which is a common loop used in JavaScript. He wrote the following code:

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

Jack was creating a loop that starts at 1 and continues as long as i is less than or equal to 10. Inside the loop, he was printing out the value of i to the console, and then he was incrementing i by 1. When he ran the code, he saw that the numbers 1 through 10 were printed out on the screen, just as he had hoped.

But Jack wasn’t satisfied yet,

He knew there were other types of loops out there, and he wanted to try them all. Next, he experimented with a “while” loop. He wrote the following code:

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

Jack was setting i to 1 before the loop, and then he was creating a loop that continues as long as i is less than or equal to 10. Inside the loop, he was printing out the value of i to the console, and then he was incrementing i by 1. When he ran the code, he saw that the numbers 1 through 10 were printed out on the screen, just as before. However, when he removed the line “i++” inside the loop, he saw that the loop ran indefinitely, even after “i” reached 10! He realized that he had forgotten to add the line that would increment i and stop the loop.

Undeterred, Jack tried a “do-while” loop next. He wrote the following code:

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

Jack was setting i to 1 before the loop, and then he was creating a loop that runs at least once and continues as long as i is less than or equal to 10. Inside the loop, he was printing out the value of i to the console, and then he was incrementing i by 1. After the loop, he was checking whether i is still less than or equal to 10, and if it is, he was running the loop again. When he ran the code, he saw that the numbers 1 through 10 were printed out on the screen, just as he had hoped.

From that day on, Jack used loops in his coding whenever he needed to repeat a block of code multiple times. He had learned the power of automation, and he never looked back.

In short, loops are an essential tool in JavaScript programming that can automate repetitive tasks and help write cleaner, more efficient code. There are different types of loops in JavaScript, each with its own advantages and disadvantages. However, loops can be tricky to use and can lead to bugs if not used correctly. To avoid these issues, it’s important to test your loops thoroughly and use descriptive variable names. Whether you’re a beginner or an experienced programmer, understanding how loops work and using them wisely can save you time and effort. So give loops a try in your own code and see the difference they can make!

Points to remember :

  • Loops are a way to repeat a block of code multiple times.
  • There are three types of loops in JavaScript: “for”, “while”, and “do-while”.
  • The “for” loop is used when you know how many times you need to repeat the code.
  • The “while” loop is used when you don’t know how many times you need to repeat the code, but you know the condition that needs to be met to stop the loop.
  • The “do-while” loop is similar to the “while” loop, but it will always run at least once before checking the condition to stop the loop.
  • When using loops, it’s important to be careful not to create an infinite loop that runs forever.
  • Loops can be nested inside each other to create more complex patterns of repetition.
  • Using loops can help you write cleaner and more efficient code by automating repetitive tasks.
  • It’s important to choose the right type of loop for the task at hand, based on the known or unknown number of repetitions and the specific conditions that need to be met for the loop to stop.
  • However, loops can also lead to bugs if not used correctly, so it’s important to test your loops thoroughly and use descriptive variable names.However, loops can also lead to bugs if not used correctly, so it’s important to test your loops thoroughly and use descriptive variable names.

I hope you found this article helpful. Thank you for reading! 😀

Happy Coding !

--

--

Ashwini Paraye

Fullstack Developer | 👨‍💻 Writing about programming concepts | React.js | JavaScript | Vue.js | Golang