引用来自https://blog.csdn.net/github_34514750/article/details/51072812
1.for
js中for循环一般用于遍历数组
//前提:cars是个数组 for (var i=0;i<cars.length;i++){ document.write(cars[i] + "<br>"); }
2.for-in
for-in一般用于循环遍历对象的属性
虽然数组在js中也是对象,技术上说,我们可以使用for-in循环数组,如下:
var x var mycars = new Array(); mycars[0] = "Saab"; mycars[1] = "Volvo"; mycars[2] = "BMW"; for (x in mycars){ document.write(mycars[x] + "<br/>"); }