Types of for loops in JavaScript
In JavaScript, there are three types of for loops:
1. Basic for loop
Syntax:
<code>for (initialization; condition; increment/decrement) {
// 循环体
}</code>
Copy after login
-
initialization: Initialization operation at the beginning of the loop.
-
condition: The condition that determines whether the loop continues. If the condition is false, the loop will be exited.
-
increment/decrement: The operation performed after each loop iteration. Used to update loop variables.
2. for...in loop
Syntax:
<code>for (variable in object) {
// 循环体
}</code>
Copy after login
-
variable:Use Variable for the key of the iterated object.
-
object: The object to be iterated. The object can be an object or an array.
3. for...of loop
Syntax:
<code>for (variable of iterable) {
// 循环体
}</code>
Copy after login
-
variable:Use A variable used to iterate over the elements in an iterable object (such as an array).
-
iterable: Iterable object. The object can be an array, string, or other data structure that supports iteration.
The above is the detailed content of There are several ways of for loop in js. For more information, please follow other related articles on the PHP Chinese website!