SoFunction
Updated on 2025-02-28

JS array traversal methods for loops and for...in

There are two ways to traverse JS arrays:

The first type: a general for loop, for example:

var a = new Array("first", "second", "third") 
for(var i = 0;i < ; i++) {
(a[i]+",");
}

Output result: fitst, second, third

The first one: use for...in to traverse, for example:

var arr = new Array("first", "second", "third") 
for(var item in arr) {
(arr[item]+",");
}

Output result: fitst, second, third