循环语句

JavaScript 中的循环语句与 C++中类似,也包含 for、while、do while 循环。

for 循环

for (let i = 0; i < 10; i++) {
    console.log(i);
}

枚举对象或数组时可以使用:

  • for-in循环,可以枚举数组中的下标,以及对象中的key
  • for-of循环,可以枚举数组中的值,以及对象中的value

while 循环

let i = 0; while (i < 10) { console.log(i); i++; }

do while 循环

do while语句与while语句非常相似。唯一的区别是,do while语句限制性循环体后检查条件。不管条件的值如何,我们都要至少执行一次循环。

let i = 0;
do {
    console.log(i);
    i++;
} while (i < 10);