1var colors = ["red","blue","green"];
2for (var i = 0; i < colors.length; i++) {
3 console.log(colors[i]);
4}
1var array = [1, 2, 3, 4, 5];
2// made an array
3
4for (var i = 0; array[i]; i++) // browse your array with this simple condition on index
5 console.log('element '+i+' = '+array[i]);
6
7//output:
8element 0 = 1
9element 1 = 2
10element 2 = 3
11element 3 = 4
12element 4 = 5
1var colors = ['red', 'green', 'blue'];
2
3 colors.forEach((color, colorIndex) => {
4 console.log(colorIndex + ". " + color);
5 });
1<!DOCTYPE html>
2<html>
3<body>
4
5<h2>JavaScript For Loop</h2>
6
7<p id="demo"></p>
8
9<script>
10const cars = ["BMW", "Volvo", "Saab", "Ford"];
11
12let i = 0;
13let len = cars.length;
14let text = "";
15
16for (; i < len; ) {
17 text += cars[i] + "<br>";
18 i++;
19}
20document.getElementById("demo").innerHTML = text;
21</script>
22
23</body>
24</html>
25
1let colors = ['red', 'green', 'blue'];
2for (const color of colors){
3 console.log(color);
4}
1let array = ["loop", "this", "array"]; // input array variable
2for (let i = 0; i < array.length; i++) { // iteration over input
3 console.log(array[i]); // logs the elements from the current input
4}