js几种for循环的几种用法

如题所述

第一种:普通for循环

for(j = 0; j < arr.length; j++) {
   
}

第二种:优化版for循环

for(j = 0,len=arr.length; j < len; j++) {
   
}

第三种:弱化版for循环

for(j = 0; arr[j]!=null; j++) {
   
}

第四种:foreach循环

arr.forEach(function(e){  
   
});

第五种:foreach变种

Array.prototype.forEach.call(arr,function(el){  
   
});

第六种:for in循环

for(j in arr) {
   
}

第七种:map遍历

arr.map(function(n){  
   
});

第八种:forof遍历(需要ES6支持)

for(let value of arr) {  
   
});

温馨提示:答案为网友推荐,仅供参考