JavaScript Switch Statement

JavaScript Switch Statement

In JavaScript, the switch statement is used to execute different code blocks based on different conditions. It is an alternative to a long chain of if/else statements.

The basic syntax of the switch statement is as follows:

Here, expression is the value that needs to be compared. value1, value2, and value3 are the possible values that expression can take. If the value of expression matches any of the cases, then the code block following that case will be executed.

The break statement is used to stop the execution of the code block after a case is matched. If none of the cases are matched, then the code block following the default statement will be executed.

Let's see an example:

In this example, we have used the switch statement to check the value of the day variable. If day is equal to "Monday", "Tuesday", or "Wednesday", then the corresponding message will be printed to the console. If day is not equal to any of these values, then the default message will be printed.

Thanks for reading and learning ❤️