When you write a script, most of the time you need to make a decision based on the values of some of your variables, or based on the values entered by the users, or even based on user actions. Decision making is a fundamental building block for all programming languages and it's similar in most of them. In this article, we discuss the if/else statement block, the switch statement, and take a look at the prompt() dialog box.
As you can see, the switch block is made up of sections, and each of them begins with the keyword case (except for the last section, which we will talk about shortly) followed by a string or numerical value, followed by a colon, then the group of statements, then the keyword break and a semicolon. The interpreter produces the value of the variable between the switch's parentheses, then goes to the first case section and evaluates this section's value. If it's the same as the switch variable's value, it executes this section's statements until it encounters the keyword break, which terminates the execution of the switch statement and executes the first statement in the script following it.
As you can see, the case value: and the break; is much more like a delimiter for each section, because all of the sections exist in one big code block. We are going to use the keyword break in the next article, in which you will really grasp its use, but for now just think of it as a way to exit the switch statement. The last section is labeled default because it's the default section that will execute if no match is found. Normally the interpreter goes to the next case section until a match is found or until the default section. This means that the default section is similar to the else block in the if/else structure. Let's modify our example to use switch instead of multiple else if statements.