JavaScript/保留字/case
外观
< JavaScript | 保留字
thecase关键字用于 switch 语句,它是复杂 if … else if … else 语句的替代方案。
代码
var array = [2, 3, 5, 7, 10, 11];
for (var i = 0; i < array.length; i++) {
switch (array[i]) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
alert(array[i] + " is a number with one digit.");
break;
default:
alert(array[i] + " is a number with several digits.");
break;
}
}
返回以下内容
2 is a number with one digit. 3 is a number with one digit. 5 is a number with one digit. 7 is a number with one digit. 10 is a number with several digit. 11 is a number with several digit.