1var data = [1, 2, 3, 4, 5, 6];
2
3// traditional for loop
4for(let i=0; i<=data.length; i++) {
5 console.log(data[i]) // 1 2 3 4 5 6
6}
7
8// using for...of
9for(let i of data) {
10 console.log(i) // 1 2 3 4 5 6
11}
12
13// using for...in
14for(let i in data) {
15 console.log(i) // Prints indices for array elements
16 console.log(data[i]) // 1 2 3 4 5 6
17}
18
19// using forEach
20data.forEach((i) => {
21 console.log(i) // 1 2 3 4 5 6
22})
23// NOTE -> forEach method is about 95% slower than the traditional for loop
24
25// using map
26data.map((i) => {
27 console.log(i) // 1 2 3 4 5 6
28})
1var colors = ["red","blue","green"];
2for (var i = 0; i < colors.length; i++) {
3 console.log(colors[i]);
4}
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 log 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});
1array = [ 1, 2, 3, 4, 5, 6 ];
2for (index = 0; index < array.length; index++) {
3 console.log(array[index]);
4}
1int[] numbers = {1,2,3,4,5};
2for (int i = 0; i < numbers.length; i++) {
3 System.out.println(i);
4}
1var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2
3//loop thrugh all of the numbers of the array and print them
4for(let i = 0; i < array.length; i++){
5 console.log(array[i]); //print all the array data in the length of i
6}
7
8//we can create some variable that store the array data and than display it
9//using for...of
10
11for(let data of array){
12 console.log(data); //print all the data that was stored in the "data" variable
13}
14