JavaScript控制语句
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript流程控制语句</title>
</head>
<body>
<script>
//示例一
var week = "Sunday";
if (week == "Sunday"){
alert("8");
} else {
alert("not 8")
}
//示例二
var week = "Sunday";
if (week = "Sunday"){
alert("7");
}else if(week == "周六"){
alert("6");
}else {
alert("not 6 and 7");
}
//示例三 case流程控制语句
var x = 1;
switch(x){
case 1:y="星期一"; break;
case 2:y="星期二"; break;
case 3:y="星期三"; break;
case 4:y="星期四"; break;
case 5:y="星期五"; break;
case 6:y="星期六"; break;
case 5:y="星期日"; break;
default:y="未定义"; break;
}
for循环 表达式一
var a=[1,"hello",true];
for (var i in a){
console.log(i);
console.log(a[i])
}
//示例四 for循环求1-100和
var ret = 0;
for (var i = 1; i < 101; i++){
ret += i;
}
alert(ret);
//示例五
for(var i = 1; i <= 6; i++){
document.write("<H"+i+">hello</H "+i+">");
document.write("<br>");
}
//示例六
obj = {"11":"111","22":[1,2]};
//示例七 对象的数据类型
alert(typeof(obj)) //object
alert(obj) //[object Object]
//示例八
for(var i in obj){
console.log(i);
console.log(obj[i]);
}
while循环
var i=1;
var ret = 0;
while(i < 101){
ret += i;
i++;
}
alert(ret)
JavaScript抛出异常
function func1() {
throw new Error("This is Error")
try{
var b = 123;
alert(b);
throw new Error("hello");
return 666;
}
catch(e){
alert(e);
}
finally {
alert("sucess!")
}
}
func1()
</script>
</body>
</html>