ES5:
1、for
let items=[1,2,3,4,5]
for(let i=0;i<items.length;i++){
console.log(items[i]) //1 2 3 4 5
}
2、forEach
items.forEach(function(item){ //不支持break、continue
console.log(item) //1 2 3 4 5
})
3、every
items.every(function(item){ //需要有return
console.log(item) //1 2 3 4 5
return true
})
4、for in
for (const index in items) {
console.log(index,items[index])
}
items.a=6
for (const index in items) { 遍历对象,会将属性遍历出来
console.log(index,items[index])
}
ES6
5、for of
let items=[1,2,3,4,5]
for (let item of items) { //可遍历自定义数据结构
console.log(item) //1 2 3 4 5
}