xxxxxxxxxx
let Hello = ['Hi', 'Hello', 'Hey'];
for(let i = 0; i < Hello.length; i++) {
console.log(Hello[i]); // -> Hi Hello Hey
}
xxxxxxxxxx
var colors = ["red","blue","green"];
for (var i = 0; i < colors.length; i++) {
console.log(colors[i]);
}
xxxxxxxxxx
var colors = ["red","blue","green"];
colors.forEach(function(color) {
console.log(color);
});
xxxxxxxxxx
const myArray = ['foo', 'bar'];
myArray.forEach(x => console.log(x));
//or
for(let i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
xxxxxxxxxx
let array = ['Item 1', 'Item 2', 'Item 3'];
for (let item of array) {
console.log(item);
}
xxxxxxxxxx
var myStringArray = ["hey","World"];
var arrayLength = myStringArray.length;
for (var i = 0; i < arrayLength; i++) {
console.log(myStringArray[i]);
//Do something
}
xxxxxxxxxx
const array = ["one", "two", "three"]
array.forEach(function (item, index) {
console.log(item, index);
});
xxxxxxxxxx
const numbers = [1, 2, 3, 4]
numbers.forEach(number => {
console.log(number);
}
for (let i = 0; i < number.length; i++) {
console.log(numbers[i]);
}
xxxxxxxxxx
const array = ["one", "two", "three"]
array.forEach(function (item, index) {
console.log(item, index);
});
Run code snippet
xxxxxxxxxx
/* ES6 */
const cities = ["Chicago", "New York", "Los Angeles"];
cities.map(city => {
console.log(city)
})
xxxxxxxxxx
//function arrayLooper will loop through the planets array
const planets = ["Mercury", "Venus", "Earth", "Mars"];
const arrayLooper = (array) => {
for (let i = 0; i < array.length; i++) {
console.log(array[i]);
}
};
arrayLooper(planets);