The switch statement is a control statement in JavaScript that allows you to execute different code blocks based on different conditions. It is similar to the if-else statement, but it provides a more concise way to write code when you have multiple conditions to check.
The switch statement evaluates an expression and compares it with multiple cases. If the expression matches a case, the code block associated with that case is executed. If none of the cases match, the code block associated with the default case is executed (if there is one).
The syntax of the switch statement is as follows:
switch(expression) { case value1: // code block to be executed if expression matches value1 break; case value2: // code block to be executed if expression matches value2 break; ... default: // code block to be executed if none of the cases match }
The expression can be any valid JavaScript expression, and the values in the cases can be any data type. The break statement is used to exit the switch statement after a case is executed. If you omit the break statement, the code will continue to execute the next case (known as "falling through").
Here are some examples of how to use the switch statement in JavaScript:
<script>
var day = "Monday";
switch(day) {
case "Monday":
document.write("Today is Monday.");
break;
case "Tuesday":
document.write("Today is Tuesday.");
break;
case "Wednesday":
document.write("Today is Wednesday.");
break;
case "Thursday":
document.write("Today is Thursday.");
break;
case "Friday":
document.write("Today is Friday.");
break;
case "Saturday":
document.write("Today is Saturday.");
break;
case "Sunday":
document.write("Today is Sunday.");
break;
default:
document.write("Invalid day.");
}
</script>
In this example, the switch statement checks the value of the variable "day" and executes the code block associated with the matching case. If the value of "day" is not one of the cases, the code block associated with the default case is executed.
<script>
var fruit = "apple";
switch(fruit) {
case "apple":
case "pear":
case "banana":
document.write("This is a fruit.");
break;
case "carrot":
case "broccoli":
case "celery":
document.write("This is a vegetable.");
break;
default:
document.write("Unknown food.");
}
</script>
In this example, the switch statement checks the value of the variable "fruit" and executes the code block associated with the matching case. However, notice that the cases for "apple", "pear", and "banana" all have the same code block. This is because you can have multiple cases with the same code block, as shown in this example.
<script>
var num = 2;
switch(num) {
case 1:
document.write("One");
case 2:
document.write("Two");
case 3:
document.write("Three");
default:
document.write("Invalid number.");
}
</script>
In this example, the switch statement checks the value of the variable "num" and executes the code block associated with the matching case. However, notice that there are no break statements in this code. This means that if the value of "num" is 2, the code will execute the code blocks for cases 2 and 3 (known as "falling through").
The switch statement is a powerful control statement in JavaScript that allows you to execute different code blocks based on different conditions. It provides a more concise way to write code when you have multiple conditions to check. By using the switch statement, you can make your code more readable and easier to maintain.