var arr = ['alon','lili','lisa','jenny','jiso'];
1. for…of 循环的是value
for(var item of arr){
console.log('yi',item);
}
// 返回的是: 'alon','lili','lisa','jenny','jiso'
2. for…in 循环的是下标
for(var index in arr){
console.log('er',index);
}
// 返回的是:0,1,2,3,4
3. forEach()
forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数。
注意: forEach() 对于空数组是不会执行回调函数的。
语法:array.forEach(function(currentValue, index, arr), thisValue)
- currentValue: 必需。当前元素
- index: 可选。当前元素的索引值。
- arr: 可选。当前元素所属的数组对象。
- thisValue: 可选。传递给函数的值一般用 “this” 值。如果这个参数为空, 则返回"undefined"。
4. JavaScript switch 语句
switch(n){
case 1:
执行代码块 1
break;
case 2:
执行代码块 2
break;
default:
与 case 1 和 case 2 不同时执行的代码
}
5. while 循环
while 循环会在指定条件为真时循环执行代码块。
while (条件){
需要执行的代码
}
6. do/while 循环
do/while 循环是 while 循环的变体。该循环会在检查条件是否为真之前执行一次代码块,然后如果条件为真的话,就会重复这个循环。
do{
需要执行的代码
}
while (条件);