1.while
while(計算的值){ // 如果為假則跳過該回圈體執行下一條程式 陳述句塊............ } var count = 0; //初始化為0 計算值為true 則每次遞增1 當遞增到5 回傳false 回圈結束 while(count<5){ count++; }
2.do while(至少執行一次)
while(計算的值){ // 如果為假則跳過該回圈體執行下一條程式 陳述句塊............ } var count = 0; //初始化為0 計算值為true 則每次遞增1 當遞增到5 回傳false 回圈結束 while(count<5){ count++; }
3.for
for(初始化,回圈條件判斷,變數更新){ 陳述句塊............ } for(var i=0;i<5;i++){ console.log(i); // 0到4的值 }
4.for in
for(變數名 in 要遍歷的物件){ 陳述句塊............ } for(var p in o){ console.log(o[p]); // 屬性名賦值給變數p 輸出每一個屬性的值 }
5.forEach() 方法
(1)定義和用法:forEach() 方法用于呼叫陣列的每個元素,并將元素傳遞給回呼函式,
注意: forEach() 對于空陣列是不會執行回呼函式的,
(2)語法:array.forEach(function(currentValue, index, arr), thisValue),其中index和arr引數可選
var numbers = [65, 44, 12, 4]; function myFunction(item,index,arr) { arr[index] = item * 10; } console.log(numbers.forEach(myFunction)); //將陣列中的所有值乘以10
6.關于break和continue
continue:中斷本次回圈,但是會繼續運行下一次回圈陳述句,(只能在回圈體使用,否則報錯)
break:退出回圈,用于終止全部回圈;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/21682.html
標籤:JavaScript
