Loops
Sometimes when we're programming, we will want to tell the computer to do something a lot of times.
As a trivial example, let's build a super simple program that prints a message to the screen:
What if we wanted to make our program print that message twice? That would be simple:
But what if we wanted to print the message 10000 times? It'd be crazy to write out the same statement 10000 times!
And what if we wanted to make the program more interactive? What if we asked the user how many times we should print the message? How could we make it so that sometimes we printed the message 4 times, and sometimes 400 times?
To make a JavaScript program perform a task repeatedly, we can utilize a feature called loops.
Loops execute a given block of code repeatedly until a certain condition is met.
There are two main types of loop that we will look at: for-loops and while-loops. We will cover how to use them, and what the different use-cases of each one are, so you'll know which one is appropriate in certain situations.