当前位置:主页 > 网页前端 > JavaScript代码 >

JavaScript switch case 的用法实例[范围]

时间:2021-09-08 09:24:50 | 栏目:JavaScript代码 | 点击:

几个例子:
复制代码 代码如下:

function case1(num){
switch(num){
case 1:
document.writeln("show 1!!");
break;
case 2:
document.writeln("show 2!!");
break;
case 3:
document.writeln("show 3!!");
break;
default:
document.writeln("show others!!");
break;
}
}
function case2(num){
switch(num){
case 1:
document.writeln("show 1!!");
//?]有break,所以???^?m?绦?case 2
case 2:
document.writeln("show 2!!");
break;
case 3:
document.writeln("show 3!!");
//?]有break,所以???^?m?绦?case 4
case 4:
document.writeln("show 4!!");
break;
default:
document.writeln("show others!!");
break;
}
}
function case3(num){
switch(num){
case 1:
case 2:
//相??於if(num==1 || num==2)
document.writeln("show 1 or 2!!");
break;
case 3:
case 4:
//相??於if(num==3 || num==4)
document.writeln("show 3 or 4!!");
break;
default:
//相??於else
document.writeln("show others!!");
break;
}
}
function case4(num){
switch(f(num)){
case 1:
case 2:
//相??於if(num==1 || num==2)
document.writeln("show 1 or 2!!");
break;
case 3:
case 4:
//相??於if(num==3 || num==4)
document.writeln("show 3 or 4!!");
break;
default:
//相??於else
document.writeln("show others!!");
break;
}
}
function f(num){
return num;
}
function case5(num){
switch(num<=2){
case true:
document.writeln("num <= 2");
break;
case false:
document.writeln("num > 2");
break;
}
}

JavaScript switch case 语句设置范围

[Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]

您可能感兴趣的文章:

相关文章