JS基础遍历方法

2018-10-18  本文已影响0人  木易先生灬

定义一个数字数组

var testArray=[100,20,50,46,80,95];

for循环

for (let i=0;i<testArray.length;i++) {
        console.log("索引",i,"元素",testArray[i]);
    }

for...in循环

for (let x in testArray) {
        console.log("索引",x,"元素",testArray[x]);
    }

for...of循环: 不需要通过索引访问

for (let o of testArray) {
        console.log("元素",o);
    }

while循环: 只知道终止循环的条件

var index=0;
while (index<testArray.length){
    console.log("索引",index,"元素",testArray[index]);
    index++;
}
上一篇 下一篇

猜你喜欢

热点阅读