1var colors=["red","blue","green"];
2for (let i = 0; i < colors.length; i++) {
3 console.log(colors[i]);
4}
1let array = ['Item 1', 'Item 2', 'Item 3'];
2
3array.forEach(item => {
4 console.log(item); // Logs each 'Item #'
5});
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 each 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});
1let i;
2for (i = 0; i < 6; ++i) {
3 console.log("Hello"); // print Hello 6 times
4}
1var listItem = [
2 {name: 'myName1', gender: 'male'},
3 {name: 'myName2', gender: 'female'},
4 {name: 'myName3', gender: 'male'},
5 {name: 'myName4', gender: 'female'},
6 ]
7
8 for (const iterator of listItem) {
9 console.log(iterator.name+ ' and '+ iterator.gender);
10 }