Several ways of traversing arrays in Javascript

1 You can use the for loop to traverse the array, the code is as follows:

var arr = [1,2,3,4,5];

for(var i=0; i<arr.length; i++){

console.log(arr[i]);

}

2 You can use the forEach method to traverse the array, the code is as follows:

var arr = [1,2,3,4,5];

arr.forEach(function(item){

console.log(item);

});

3 You can use the for...in loop to traverse the array, the code is as follows:

var arr = [1,2,3,4,5];for(var i in arr){

console.log(arr[i]);

}

4 You can use the for...of loop to traverse the array, the code is as follows:

var arr = [1,2,3,4,5];

for(var item of arr){

console.log(item);

}

Guess you like

Origin blog.csdn.net/JohnJill/article/details/129220560