https://www.cnblogs.com/baiyunke/p/7821299.html
写的很好,自己收藏自己用
while循环
var num = 1;//1、声明循环变量
while (num<=10){//2、判断循环条件;
document.write(num+"<br />");//3、执行循环体操作;
num++;//4、更新循环变量;
}
do-while循环
var num = 10;
do{
document.write(num+"<br />");//10 9 8 7 6 5 4 3 2 1 0
num--;
}while(num>=0);
document.write(num);//-1
for循环
for (var num =1; num<=10; num++) {
document.write(num+" <br />"); //1 2 3 4 5 6 7 8 9 10
}
for-in循环
//声明一个Peson类
function Person(){
this.name = "张三";
this.age = 14;
this.func1 = function(){
}
}
//实例化这个类
var zhangsan = new Person();
//使用for-in遍历这个对象
//keys是键名
for(keys in zhangsan){
console.log(zhangsan[keys])
}
for-of循环
var arr = ['a', 'b', 'c', 'd'];
for (let a in arr) {
console.log(a); // 0 1 2 3
}
for (let a of arr) {
console.log(a); // a b c d
}