// 0
// 1
// 2
- ES6 之后出现的语法,与
for
回圈相比写法较简洁
- 与
for
回圈一样,可以搭配 break
、continue
和 return
使用
const arr = ["apple", "orange", "banana"];
for (const item of arr) {
if (item === "orange") continue;
console.log(item);
}
// apple
// banana
{% outerLink href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in" text="for...in" /% }
- 遍历的是
键值 (key)
,而在数组中的 键值 (key)
就是 索引 (index)
const arr = ["apple", "orange", "banana"];
for (const item in arr) {
console.log(item);
}
// 0
// 1
// 2