array = [ 1, 2, 3, 4, 5, 6 ]; for (index = 0; index < array.length; index++) { console.log(array[index]); }
const fruits = ['mango', 'papaya', 'pineapple', 'apple']; // Iterate over fruits below // Normal way fruits.forEach(function(fruit){ console.log('I want to eat a ' + fruit) });
var colors = ["red","blue","green"]; for (var i = 0; i < colors.length; i++) { console.log(colors[i]); }
const avengers = ['thor', 'captain america', 'hulk']; avengers.forEach((item, index)=>{ console.log(index, item) })
let words = ['one', 'two', 'three', 'four']; words.forEach((word) => { console.log(word); }); // one // two // three // four
var colors = ["red", "blue", "green"]; colors.forEach(function(color) { console.log(color); });
let numbers = ['one', 'two', 'three', 'four']; numbers.forEach((num) => { console.log(num); }); // one //two //three // four
const array1 = ['a', 'b', 'c']; array1.forEach(element => console.log(element));
let array = ['Item 1', 'Item 2', 'Item 3']; array.forEach(item => { console.log(item); // Logs each 'Item #' });
users.forEach((user, index)=>{ console.log(index); // Prints the index at which the loop is currently at });
var fruits = ["apple", "orange", "cherry"]; fruits.forEach(getArrayValues); function getArrayValues(item, index) { console.log( index + ":" + item); } /* result: 0:apple 1:orange 2:cherry */