Loops are used to execute the same block of code again and again, as long as a certain condition is met. The basic idea behind a loop is to automate the repetitive tasks within a program to save the time and effort. JavaScript now supports five different types of loops:
- while — loops through a block of code as long as the condition specified evaluates to true.
- do…while — loops through a block of code once; then the condition is evaluated. If the condition is true, the statement is repeated as long as the specified condition is true.
- for — loops through a block of code until the counter reaches a specified number.
- for…in — loops through the properties of an object.
- for…of — loops over iterable objects such as arrays, strings, etc.
In the following sections, we will discuss each of these loop statements in detail.
JavaScript Loops - Code This, Not That
Javascript-джедай - Циклы