4.2 Switch...case

Now let's take a look at the conditional switch case instruction, let's start by declaring a variable that we'll call role. Let's imagine that it represents the role of the current user. We want to see if it's a guest or a moderator or an administrator. We can easily implement this logic, using a bunch of if and else statements. But there is another way, using switch case. So let's take a look. We start with this switch statement, we add brackets, but instead of adding a condition here, we add a variable, in this case, we're going to add the variable role. Now we add braces, and then we add one or more case statements, each case statement is used to compare the value of this variable with something. So this is our first case. If role is equal to guest, so I compare the value role with guest, we add a colon, and then after that we add one or more statements. So we can make a console.log of guest. Here we add a variable, in this case we're going to add the role variable. Now we add braces, then we add one or more case statements, each case statement is used to compare the value of this variable with something. So this is our first case. If role is equal to guest, so I compare the value role with guest, we add a colon, and then after that we add one or more statements. So we can make a console.log of guest. Here we have to add a break statement. So we jump out of this switch block. Otherwise, the other instructions here will be executed. So we need to add another case statement. If this user is a moderator, then we show on the console Moderator, And we add a break, in this case, if I don't add this break statement here, and the role is equal to guest, these two console.log statements will be executed. That's why we need to add a break to get out of this block. Now, we can have as many case statements here, there is no limitation. As an option, if none of these cases match the value of the variable initiated at the beginning, we can have a default statement and execute one or more Instructions So we make a user console log. For the default instruction we don't need to make a break, because at this point the control will automatically exit this block. We save the changes and we get unknown user, because the role value is not defined and does not match any of these case statements. Now, if I change that to guest, we save the changes, and we get guest user, and similarly, if I change the moderator, we get the moderator user message. So this is what we need to remember. With switch and case, we can compare the value of a variable, with a bunch of other values. Now they don't have to be strings, they can be numbers or even booleans. But the use of booleans here is less common. Because if you want to compare the value of a variable with true or false, it makes more sense to use an if statement. Now, we're going to implement this exact logic by using if and else statements. So, if role is equal to guest, note that I'm using strict equality here, so triple equal sign, and that means we're checking the type and value of this variable against what we have on the right side. So if role is a guest, then we do a console. Log of the guest or the guest user. Otherwise, if the role is equal to the moderator, then we make a console.log of the moderator and if the role is something else, then we display an unknown user. Now, we can see that the implementation method with the if and else is cleaner and shorter, we don't have all these break or default statements, or these braces, these are extra lines in the code. So it doesn't mean that you should never use Switch case, it depends on what you have asked for in the instruction. That's it for this demonstration on the conditional statements switch and case, see you on the next demo on the for loops.