一、流程控制
包括:if-else、switch、条件运算符?。
<!doctype html> <html> <head> <meta charset="gb2312"> <title>实验</title> </script> </head> <body> <script language="javascript"> var p; var person = new Object(); person.name = "张三"; person.age = 28; person.sex = false; person.phone = "13815062893"; person.email = "zhangsan@163.com"; //输出属性 for(p in person) { console.log(person[p]); } //switch 流程控制 switch (person.sex) { case true: console.log("性别:男!"); break; case false: console.log("性别:女!"); break; default: console.log("性别:不详!"); } // if-else 流程控制 if(person.sex == true) console.log("性别:男!"); else console.log("性别:女!"); // ? 条件运行符 console.log( (person.sex == true) ? "性别:男!":"性别:女!"); </script> </body> </html>
二、循环控制
包括:for、for in、while、do while 循环。
<!doctype html> <html> <head> <meta charset="gb2312"> <title>实验</title> </script> </head> <body> <script language="javascript"> var str = "a,b,c,d,"; //用字符串的split方法,拆分字符串 var t = str.split(","); console.log(t[0],t[1],t[2],t[3]); //for 循环 for(var i=0; i < t.length ; i++) { console.log(t[i]); } //for in 循环 for(i in t) { console.log(t[i]); } //while 循环 var i = 0; while(i < t.length) { console.log(t[i]); i++; } //do-while 循环 i = 0; do { console.log(t[i]); i++; } while (i < t.length); </script> </body> </html>