Hello in this JavaScript video I am going to show you the switch statement. So the switch statement is sort of similar to the if statement. If you essentially just want to check if that a variable equals some you know value and then elsif that variable equals in overvalue elsif the variable equals no value and etc etc. Otherwise have some sort of default for back then a switch is fantastic. I saw it on that sort of scenario, a switch is useless if you want to send the check in against a single variable and it is a simple equals comparison, which is pretty good. So let me create a variable so var.
I will call this awesome var and find the value of full Now to do so with them, because then you put open curly bracket inside here, you put the variable that you're checking so forth, and then you put curly braces, and then you put something, put a case. And here is where you put what you want to check your variable is equal to, so let's put zero and we put cola. And then what you do is put the key word break, and any code that you want to run, if awesome boy equals zero, you put in between the case and the break. So I'm simply going to console dot log zero, and I'll do a six conditions for six. Okay, so 123 four and five, I just need to update the value. Wow.
345 Save it. And if I reload, see what we get? He says this is four because it goes here what's involved? You know, it assigns a value of four to it. And it checks if awesome y equal to zero. Nope.
It also equal to one Nope, is equal to No, free. No. is equal four. Yes, it is. The Death is four. And yeah, and that is it.
Okay, so if you want some for default fallback, so if I turn this to booty, which isn't one of the valid cases, one of the valid checks, you reload and see nothing appears if you want to default. You just put the keyword default colon at the end, and you can put console dot log into Lanza TV to see Want to save the default? value? Okay, and what you can do, you put a break here for the default at the end is sort of redundant, or we'll put it there and I'm going to explain in a second exactly what that actually does. Before we load it up, we can say this is the default one. So that is really for the switch statement.
The only one last thing I want to explain is the break case. So let me show you what happens if zero and it says this is zero. Okay, that's pretty cool. Let's actually commentate this break this spray this break. That should be enough to illustrate the point for reload. We get defeat zero.
This is one two and three my thinking but awesome boys only zero. What happens is if it gets into a case, so we get into cases zero doesn't have to be the first case So let me demonstrate auto case. One. It gets into case one, let's change it back to them. So it gets into this first case, Princess despair code, because it doesn't see a break statement. It just keeps going.
But it doesn't actually continue with a check. It just goes to the code inside the next check, does no break. He goes to the next checks, code, no break, goes to the next checks code. And then there is a break my thinking, when would you actually want something like this. And the scenario is when you when you actually have some code that you want to run for, let's say multiple conditions, and the way you could actually do that is by doing something like this. So maybe you want the same code for cases 012 and three There you go, you'll run the same code.
Let me just change this back. So that's it for the switch statement. If you have any questions, feel free to pop me a message and I look forward to seeing you in the next video.