JavaScript basics
JavaScript Switch case writing
JavaScript Switch case is used to judge various situations and decide which part of the code to execute. To use Switch, you must first set various conditions (case). In addition to various conditions, you can also add a default block. If none of them match, the code of default can be executed.
JavaScript Switch syntax example
<script language="javascript">
var Number=0;
switch (Number) {
case 0:
x="Number 是 0";
break;
case 1:
x="Number 是 1";
break;
default:
x="沒有符合的條件";
}
document.write(x);
</script>
From the example, we can see that each case is matched with a break, which means that when the case is matched, after the code is executed, the switch will be jumped out through the break and no other code in the switch will be executed. In this example, because Number is set to 0 by default, the final output value of document.write is "Number is 0". We also added the default block. If the initial value of Number in the example is set to other, it is not within the conditions For example, Number=3, it will output "No conditions met".var Number=0;
switch (Number) {
case 0:
x="Number 是 0";
break;
case 1:
x="Number 是 1";
break;
default:
x="沒有符合的條件";
}
document.write(x);
</script>
Post a Comment
0 Comments