We often need to repeat actions.
For example, outputting goods from a list one after another or just running the same code for each number from 1 to 10.
Loops are a way to repeat the same code multiple times.
The for…of and for…in loops
A small announcement for advanced readers.
This article covers only basic loops: while, do..while and for(..;..;..).
If you came to this article searching for other types of loops, here are the pointers:
- See for…in to loop over object properties.
- See for…of and iterables for looping over arrays and iterable objects.
Otherwise, please read on.
The “while” loop
The while loop has the following syntax:
while (condition) {
// code
// so-called "loop body"
}