for in :遍历方法,可遍历对象和数组
for of :遍历方法,只能遍历数组,不能遍历对象
先看for in:
const obj = { name: '林三心', age: 22, gender: '男' }
const arr = [1, 2, 3, 4, 5]
for(let key in obj) {
console.log(key)
}
name
age
gender
for(let index in arr) {
console.log(index)
}
0 1 2 3 4
再看 for of:
for(let item of arr) {
console.log(item)
}
1 2 3 4 5