条件语句
if (条件){语句1}else{语句2}
var isMale = false;
if(isMale){
document.write('男');
}else{
document.write('女');
}
//女
if (条件){语句1}else if(条件2){语句2}else{语句3}
var score = 65;
if(score > 70){
document.write('A');
} else if(score >= 60){
document.write('B');
} else {
document.write('C');
}
//B
switch(表达式){
case 值1:语句
break;
case 值2:语句
break;
default:语句
}
var degree = 'B';
switch(degree){
case 'A':
document.write('优秀');
break;
case 'B':
document.write('良好');
break;
default:
document.write('不合格');
}//良好
循环语句
while(表达式){语句}
var i = 1;
while(i <= 10){
document.write(i);
i++;
}
//12345678910
do(语句)while(表达式)
var i = 11;
do{
document.write(i);
i++;
}
while(i<=10)
//11
for(初始化;循环条件;更新表达式){语句}
for(var i = 1;i <= 10;i++){
document.write(i);
}
//12345678910
break,continue
for(var i = 1; i <= 10;i++){
if(i == 5){break;}
document.write(i);
}
//1234
for(var i = 1; i <= 10;i++){
if(i==5){continue;}
document.write(i);
}
//1234678910
for(属性名 in 对象){语句}
var cat = {
name:'kitty',
age:2,
mew:function(){
console.log(喵喵喵);
}
}
for(var p in cat){
document.write(p);
}
//name age mew
with语句
with(表达式){语句} 将当前语句的代码的作用域指向到一个特殊的对象上
var kitty = {
age:3,
friend:function(){
name:'snoopy',
age:2,
}
}
document.write(kitty.friend.name + '\'s age is' + kitty.friend.age);
//snoopy's age is2
with(kitty.friend){
document.write(name + '\'s age is' + age);
}
//snoopy's age is2
异常捕获语句
try{
语句
}catch(exception){
语句
}finally{
语句
}
try{
document.write(notDefined);
} catch(error){
alert(error);
} finally {
alert('finally');
}
//ReferenceError
//finally