April 2018
Beginner to intermediate
426 pages
10h 19m
English
Loops are often used when we work with arrays (which are the subject of the next chapter). Specifically, we use the for loop in our algorithms.
The for loop is the same as in C and Java. It consists of a loop counter that is usually assigned a numeric value, then the variable is compared against another value (the script inside the for loop is executed while this condition is true), and finally, the numeric value is increased or decreased.
In the following example, we have a for loop. It outputs the value of i on the console, where i is less than 10; i is initiated with 0, so the following code will output the values 0 to 9:
for (var i = 0; i < 10; i++) {
console.log(i);
}
The next loop construct we will look at is the while loop. ...