This article mainly introduces how to use the for Loop in JavaScript. It is the basic knowledge in the JS getting started learning. If you need it, you can see that the while loop has different variants. This chapter introduces another popular loop called for loop.
For Loop
A for Loop is the most compact form of a loop and consists of the following three important parts:
- The initial value of the counter. Before the initialization statement execution cycle starts.
- Test statement to test whether the given condition is true or false. If the condition is true, the code given in the loop to be executed will exit.
- Loop statement, which can increase or decrease the counter.
One row in all three parts can be separated by a semicolon.
Syntax
for (initialization; test condition; iteration statement){ Statement(s) to be executed if test condition is true}
Example:
The following example shows a basic for loop: