JS array traversal method for loop and for... The in

  • 2020-03-30 03:37:17
  • OfStack

JS array traversal methods have two:

The first is a general for loop, such as:


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

Output: fitst,second,third

First: use for... In this way of traversing, for example:


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

Output: fitst,second,third


Related articles: