The JavaScript for loop is used for the codes that are desired to work until the number of repetitions.
JavaScript cycles are used for continuous operation of the same code depending on the condition.
In the example, the sequence sequence number is reused in ascending order.
var FruitList = ["apple", "orange", "banana", "pineapple","strawberry","watermelon"]
alert(FruitList[0]);
alert(FruitList[1]);
alert(FruitList[2]);
alert(FruitList[3]);
alert(FruitList[4]);
alert(FruitList[5]);
Instead of reuse;
var FruitList = ["apple", "orange", "banana", "pineapple","strawberry","watermelon"]
var i, listLength = FruitList.length;
for (i = 0; i < listLength; i++) {
alert(FruitList[i]);
If the array had 300 or 1000 elements instead of 7 elements?
JavaScript for loop
The JavaScript for loop is typically used for the execution of the code block until the number of repetitions specified by the Condition.
The use of the JavaScript for loop is as follows.
for (Command1; Condition; Command2){
//work code
}
Command 1 contains commands to run without looping.
The condition is used to specify the working condition of the loop code block.
Command 2 contains commands to run after the loop code blocks.
Example of collection with JavaScript for loop;
var i, total = 0;
for (i = 0; i < 5; i++) {
total += i;
}
alert(total)
The following operations are performed in the example.
Command 1 sets the loop start value (i = 0).
The condition determines the condition required for the cycle to work (i <5).
Command 2 increases the i variable value after the loop code blocks (i ++).
Leave a comment