for循环
最为常用的遍历方法,不过多赘述,可以用break跳出循环
for in循环
最为常见的对象遍历方法,不过多赘述 break可跳出
for(let key in obj){
console.log(obj[key])
}
for of循环
for…of循环可以使用的范围包括数组、Set 和 Map 结构、某些类似数组的对象(比如arguments对象、DOM NodeList 对象)、后文的 Generator 对象,以及字符串。
break可跳出
阮一峰的es6教学
forEach()
数组循环
const array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element));
// expected output: "a"
// expected output: "b"
// expected output: "c"
除了抛出异常没有办法终止循环
while循环
先判断后执行
var num = 1;//1、声明循环变量
while (num<10){//2、判断循环条件;
console.log(num);//3、执行循环体操作;
num++;//4、更新循环变量;
}
do while循环
线执行后判断,比while多执行一次
var num = 10;
do{
console.log(num);//10 9 8 7 6 5 4 3 2 1 0
num--;
}while(num>=0);
console.log(num);//-1
map()
数组循环
var array1 = [1,4,9,16];
const map1 = array1.map(x => x *2);
console.log(map1);