1var colors = ["red","blue","green"];
2for (var i = 0; i < colors.length; i++) {
3 console.log(colors[i]);
4}
1const avengers = ['thor', 'captain america', 'hulk'];
2avengers.forEach((item, index)=>{
3 console.log(index, item)
4})
1var colors = ['red', 'blue', 'green'];
2
3colors.forEach(function(color) {
4 console.log(color);
5});
1array = [ 1, 2, 3, 4, 5, 6 ];
2for (index = 0; index < array.length; index++) {
3 console.log(array[index]);
4}
1const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
2
3// Iterate over fruits below
4
5// Normal way
6fruits.forEach(function(fruit){
7 console.log('I want to eat a ' + fruit)
8});
1var fruits = ["apple", "orange", "cherry"];
2fruits.forEach(getArrayValues);
3
4function getArrayValues(item, index) {
5 console.log( index + ":" + item);
6}
7/*
8result:
90:apple
101:orange
112:cherry
12*/