1var colors = ["red","blue","green"];
2for (var i = 0; i < colors.length; i++) {
3 console.log(colors[i]);
4}
1let array = ['Item 1', 'Item 2', 'Item 3'];
2
3// Here's 4 different ways
4for (let index = 0; index < array.length; index++) {
5 console.log(array[index]);
6}
7
8for (let index in array) {
9 console.log(array[index]);
10}
11
12for (let value of array) {
13 console.log(value); // Will log value in array
14}
15
16array.forEach((value, index) => {
17 console.log(index); // Will log each index
18 console.log(value); // Will log each value
19});
1var colors = ['red', 'green', 'blue'];
2
3 colors.forEach((color, colorIndex) => {
4 console.log(colorIndex + ". " + color);
5 });
1var txt = "";
2var numbers = [45, 4, 9, 16, 25];
3
4numbers.forEach(function(value, index, array) {
5 txt = txt + value + "<br>";
6});
7
1var colors = ["red", "green", "blue"];
2for(var i = 0; i < colors.length; i++){
3 console.log(colors[i]);
4}
1var yearStart = 2000;
2var yearEnd = 2040;
3
4var arr = [];
5
6for (var i = yearStart; i < yearEnd+1; i++) {
7 arr.push(i);
8}
9