Loops
Explore how to use loops in JavaScript to repeat code based on conditions, including while and do-while loops. This lesson helps you understand loop structures, avoid infinite loops, and apply loops to practical scenarios like counting down and running repetitive processes effectively.
We'll cover the following...
Computers are great at carrying out repetitive tasks over and over again. They never get bored and will follow the instructions exactly every time. Loops are a way of getting a program to repeat a piece of code according to certain conditions.
What’s a loop?
A loop is a piece of code that will continue to run until a certain condition is met.
To illustrate this, here’s a diagram that shows the countdown to a rocket blastoff as a loop.
As we can see, we start at , say the number out loud, subtract one from the number and then check to see if we’ve reached zero. If we haven’t reached zero, we loop back and repeat the process over again, so we continue to say the number out loud, then subtract ...