JavaScript循环语句输出1到100的五种方法有哪些!
发布网友
发布时间:2022-04-06 14:56
我来回答
共1个回答
热心网友
时间:2022-04-06 16:25
// 1 for循环
for (i=1;i<=100;i++){ alert (i)}
//2 while循环
var i=1
while (i<=100){
alert(i);
i++;
}
//3 do while 循环
var i = 1;
do{
alert(i
i++;
}while(i <= 100);
//for in 和 forEach 用于数组的循环遍历
//4 for in
var arr1=[1,2,3,4,5,6,...99,100] ;
for (i in arr1){
alert (i);
}
//5 foreach
var arr1=[1,2,3,4,5,6,...99,100] ;
arr1.forEach(function(i){ alert(i)});