The for loop in JavaScript is a loop structure used to repeatedly execute a block of code. It works through three phases: initialization, conditions, and increment, which are executed at the beginning of the loop, before it starts, and after it completes.
Using for loop in JavaScript
The for loop is a method in JavaScript that is used to repeatedly execute a specific block of code. Loop structure. The syntax is as follows:
<code>for (initialization; condition; increment) { // 循环体:要重复执行的代码 }</code>
Working principle:
Example:
<code class="javascript">// 循环 5 次 for (let i = 0; i < 5; i++) { console.log(`循环第 ${i + 1} 次`); } // 循环数组 const arr = [1, 2, 3, 4, 5]; for (let j = 0; j < arr.length; j++) { console.log(arr[j]); }</code>
Other notes:
break
statement to exit the loop early. continue
statement to skip the current loop and continue executing the next loop. The above is the detailed content of How to use for loop in js. For more information, please follow other related articles on the PHP Chinese website!