1var colors = ['red', 'green', 'blue'];
2
3 colors.forEach((color, colorIndex) => {
4 console.log(colorIndex + ". " + color);
5 });
1var myStringArray = ["hey","World"];
2var arrayLength = myStringArray.length;
3for (var i = 0; i < arrayLength; i++) {
4 console.log(myStringArray[i]);
5 //Do something
6}
1<!DOCTYPE html>
2<html>
3<body>
4
5<h2>JavaScript For Loop</h2>
6
7<p id="demo"></p>
8
9<script>
10const cars = ["BMW", "Volvo", "Saab", "Ford"];
11
12let i = 0;
13let len = cars.length;
14let text = "";
15
16for (; i < len; ) {
17 text += cars[i] + "<br>";
18 i++;
19}
20document.getElementById("demo").innerHTML = text;
21</script>
22
23</body>
24</html>
25
1const myArray = [{x:100}, {x:200}, {x:300}];
2
3const newArray= myArray.map(element => element.x);
4console.log(newArray); // [100, 200, 300]