JavaScript—判断语句

201 阅读1分钟

if判断

if():真判断

只有当指定条件为true时,使用该语句来执行代码

if(condition){
    //当条件为true时执行的代码
}

if(){}else{}:非真即假判断

条件为true时执行if中的代码,在条件为false时执行else中的代码

if(condition){
    //当条件为true时执行的代码
}else{
    //当条件为false时执行的代码
}

if(){}else if(){}else{}:选择判断

选择多个代码块之一来执行

if(condition1){
    //当条件1true时执行的代码
}else if(condition2){
    //当条件2true时执行的代码
}else if(condition3){
    //当条件3true时执行的代码
}else{
    //当以上条件都不满足时执行的代码
}

swtich判断

swtich():选择判断

选择要执行的多个代码块之一

swtich(n){
    case 1:
        //执行代码块1
        break
    case 2:        //执行代码块2
        break
case 3:
        //执行代码块3
        break......

default:        //当以上条件都不满足时执行的代码
}