switch (expression)
case value: statement;
break;
case value: statement;
break;
case value: statement;
break;
case value: statement;
break;
...
case value: statement;
break;
default: statement;
var BLUE = "blue", RED = "red", GREEN = "green";
switch (sColor) {
case BLUE: alert("Blue");
break;
case RED: alert("Red");
break;
case GREEN: alert("Green");
break;
default: alert("Other");
}
這里,switch 語句用于字符串 sColor,聲明 case 使用的是變量 BLUE、RED 和 GREEN,這在 ECMAScript 中是完全有效的。
更多建議: