This lesson is going to be about the JavaScript switch statement, and the name really gives it away,
it gives you the ability to switch between different cases.
And if the case is true, then you can run a block of code.
Which statement?
There's more information, of course, at the Mozilla Developer Network about the switch statement.
So basically it evaluates an expression and finds the matching case.
So for the sample here that they have, they have an expression.
So this is a string equal to papayas and the switch matches the expression.
So the expression goes within the rounded brackets and then you've got curly brackets running the block,
a code checking to see all the different cases.
So until that case translates to be true, then that's when it's going to start running the code.
And notice as well that there are breaks in between that breaks that switch block of code.
So if we were to run for papayas, papayas was up here and we had no break, we would run both blocks
of code until we hit a break.
There's also an option for a default.
So when you run this, you can see that it outputs the correct corresponding value that's within the
case.
It gives you the option to make a cleaner conditional statement instead of having to put a bunch of
else.
If you also have the default as well that you can utilize.
The challenge within this lesson is to select a response depending on the name of the person in the
variable that's being called where we've got person.
Person is Lowrance.
The switch is taking that expression and comparing it with all the different cases so you can go ahead
and pause the video, work through this example and I'll show you the solution coming up that we need
to do is to find the person I'm going to use the same as within the example where we're going to be
using the name Lawrence.
And then we've got our switch statement taking in the expression.
So this expression could be any type of variable.
We could be also looking for a number as well.
And then running through the switch.
We're setting up the cases.
So it's case and then the value of the case.
So we'll take a look for John.
And then here we've got the code that we're going to be running and you can come up with any type of
message you want there.
And then don't forget the break as well.
We also have the case, so we're checking to see if equal to Lowrance.
And if it is, then we've got the colon there and then we're going to run our block of code.
And I'm also going to do one without a break and we'll add in the break afterwards so you can see that
what happens if we don't include the break, add in the default as well.
And I'll do a break here if none are true and we can just output.
Nobody found refresh.
Found it.
And hi, Steve.
That's because we don't have a break there, so we can go and we can add in the break.
And actually the break should be between here.
So the break should be just after the block of code that we want to run.
So this gives us the option to add in as many cases as necessary.
There's a lot cleaner than if we were looking for a person is equal to John else if person is equal
to Lawrence, else if person is equal to Steve and so on.
So it gives us is the section that you've all been waiting for and that's functions.
And this is where we can really power up with what we do in JavaScript.