The switch conditional is an alternative to the if conditional, you would mainly use it in a situation where you would have multiple if else statements together similar to what we had in the last movie. So we're going to take a look at the exact same problem we're working on in the last. So I'm going to go to sublime. And here we have our app j. s file, our JavaScript file. This is what we did in the previous movie. Several if else, else if statements together to determine what day of the week it is.
Let's now take a look at how we would do the same thing with a switch statement. Go ahead and delete this. The switch statement begins with the keyword switch. Then we have parentheses where we placed the conditional expression. In this case, we just want To check what the variable day is equal to. Following that, we then do our curly braces.
So the entire switch statement is enclosed within curly braces. that's similar to an if statement or if else statement. Now we get into the main differences. Inside of the switch statement. There are individual case statements. And it's the case statements that determine whether the expression which we put in the conditional, what is equal to.
So for example, case, zero, and then we do a colon. And so if it's equal, if they happens to be equal to zero, then it will execute any statements that are underneath this case statement. So we go ahead and indent that. We're just going to do console dot log. Same type of statement today is Sunday. I'm going to close out with an excellent x, we'll close that with a semicolon.
Now we could obviously continue to enter additional statements if there are other things we needed to do at this point. However, one thing that is required is when we're done with a particular case statement, we need to enter the keyword break. That causes the execution to stop at that point. We've found the value we want, we execute the statement or statements and then we stopped executing within the switch and it drops out of the switch. That's the purpose of the break. Now let's enter another case statement.
Case one colon console dot console dot log. Today is Monday. So Michael And then another break statement. Great. So all the case statements will be very similar to that. Let me go ahead and copy this case statement here.
We're going to paste a bunch of them in and then I'll modify it to represent all the days of the week. So we have CASE CLOSED 12345 and six. And so that will be Tuesday. Wednesday, Thursday, Friday and Saturday. All right. Now one more thing that's a part of the switch statement, if we want to include it as we can have a default.
Whoops, that would be entered like this. And then we would have our statements here. Now the purpose of default is, if nothing matches any of the case statements, it will execute the statements underneath default, we would need to have a break as well. Now we know there's only seven days in a week. So we don't need a default in this case, so I'm going to eliminate that. All right, let's go ahead and save this and see how it works.
Once again, I'm going to copy the file path so I can enter that into the browser and then open the console and today is Tuesday, and that is the correct date. Now before we leave the switch statement, I want to show you what would happen if we did not have any the break statements. The break keyword entered This will let you know the importance of the break keyword. I'm gonna go ahead and save that. And then let's refresh this, bring up the JavaScript console and see what happens. See it, it found the correct case statement to today is Tuesday.
However, because there were no break statements, it continued to execute the statements in the case statements below that. So today is Wednesday, it is Thursday, Friday and Saturday it printed all of those. And so that is the reason that we need to have the break statements within our switch conditional. Now whether you choose to use if conditionals or a switch conditional is pretty much up to you there are certain situations where the switch is much more readable and much more manageable. And that is the reason it is it is used in those types of situations. But as you can see, they do accomplish the same thing.
All right, let's continue on.