JavaScript中提供了这些循环语句:
for
语句do...while
语句(至少执行一次)while
语句labeled
语句break
语句continue
语句for...in
语句for...of
语句
label
语句
label :
statement
用 label
标识一个循环, 然后使用 break
或者 continue
来指出程序是否该停止循环还是继续循环
for...in
语句
循环一个指定的变量来循环一个对象所有可枚举的属性
不建议用其迭代数组
for...of
语句
let arr = [3, 5, 7];
arr.foo = "hello";
for (let i in arr) {
console.log(i); // 输出 "0", "1", "2", "foo"
}
for (let i of arr) {
console.log(i); // 输出 "3", "5", "7"
}
// 注意 for...of 的输出没有出现 "hello"