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});
1//for
2 str='hello'
3 for (var i = 0; i < str.length; i++) {
4 console.info(str[i]);
5 }
6
7//for...of
8 let result1 = '';
9 for (let letter of str) {
10 result1 += letter;
11 }
12 console.log(`for...of : ${result1}`)
13
14//forEach
15
16 // ES6 version.
17 let result2 = '';
18 str.split('').forEach(letter => {
19 result2 += letter;
20 });
21 console.log(`forEach ES6 : ${result2}`)
22
23 //or
24 var result3 = '';
25 str.split('').forEach(function(letter) {
26 result3 += letter;
27 });
28 console.log(`forEach standard : ${result3}`)
29
30//for...in
31 var result4 = '';
32 for (var letterIndex in str) {
33 result4 += str[letterIndex];
34 }
35 console.log(`for...in : ${result4}`)
36//map
37 // ES6 version.
38 var result5 = '';
39 str.split('').map(letter => {
40 result5 += letter;
41 });
42 console.log(`map ES6 : ${result5}`)
43
44 //or
45 let result6 = '';
46 str.split('').map(function(letter) {
47 result6 += letter;
48 });
49 console.log(`map standard : ${result6}`)
50
51
52
53