1.for循环最基本的循环方式,可以return终止。
for(var i = 0;i < 3; i++){
console.log(i)
}
2.for of循环作为ES6新增的循环方法。它可以正确响应break、continue和return终止。for-of支持数组,还支持类数组对象。
let arr = ['110','yyds','jjkk'];
for(let item of arr){
console.log(item); //item指的是数组每一项。
}
//输出 '110' 'yyds' 'jjkk'
3.forEach循环没办法用break跳出循环。没有 return 返回值。
let arr = ['110','yyds','jjkk'];
arr.forEach(function (value, index) {
//value是每一项,index是索引
console.log(value,index);
});
4.map循环,将原有的数组映射成一个新数组,不操作原数组,返回新数组。map支持return;
let arr = ['110','yyds','jjkk'];
arr.map(function(value,index){
//value每一项,index索引
console.log(value,index);
});