- [Instructor] The if else statement is a useful tool,
but in many cases
it's not the right tool for the job.
Let's say you want to output different values
depending on how old the backpack is.
So if it's under 30 days old, it's new.
If it's under one year old, it's lightly used.
If it's one year or older, it's used.
And if it's more than three years old, it's old.
We could do this by nesting conditional statements
inside one another.
But as you can see from the code example
it gets clunky and hard to read.
Here we're saying,
if the age is over 30,
and if the age is over 365,
and if the age is over 1095,
then it's old.
Otherwise it's used, otherwise it's lightly used.
Otherwise it's new.
This is not only hard to write,
it's also hard to read.
Instead, when we want to get different results
depending on various conditions,
we can use a switch statement.
Here if we look at the code example,
we provide an expression
which is effectively a value.
And then we provide a series of case clauses
expressed with case,
and then the clause.
And each of these case clauses
describes a specific condition.
The switch statements
then starts at the top
and goes through each of the cases,
and we'll stop when it finds one
that matches the expression
and then output whatever content is inside that case.
And then go to break,
meaning it stops the switch statement from continuing.
Then at the bottom,
we usually have a default statement.
So if none of the conditions are met,
we use,
we do whatever it says in the default statement.
As this example on MDN shows,
switches typically used for literal matching.
So here we're looking for the word papayas
and then when we hit the word papayas,
we output the content.
But we can also use this same switch expression
to test for ranges
or testing for different conditions
by coming at the problem
from a slightly different angle.
Let me show you how this works.
So first I'll wipe out all these if statements
'cause we don't need them anymore.
This used status function
sets up two lets,
first age that grabs the backpack age
and then also a description.
And then it just outputs the age
and the description down here.
Those both show up in the console in the browser.
Okay. So now we need to redefine the description
based on age.
So we'll set up a new switch statement.
It's just called switch.
Then we set up the expression we want to test against
and here we're going to test against the expression true.
Then we can use conditional statements
and if the conditional statement is true
then we pick that case to output the content.
All the cases are put inside curly brackets
and then we just name each case in turns
we'll say case.
First we want to see if the age
is smaller than 30.
That's our first condition.
If that's the case, colon,
we output something in this case,
description is set to new,
then because we've met our condition we break
and that means we stop the switch statement
from continuing to run.
If this first case,
so this condition is not met,
so it's not true.
Then the object is slightly older.
So that means we can set up a new case.
And this time we're going to use two statements.
So we'll say age
is bigger than or equal to 30
and two ampersands
age is smaller than 365.
So this will give us the condition
where the age is over 30 but under 365.
And only in that circumstance,
will this return true.
If that's the case,
meaning the bag is less than a year old
then the description should be lightly used,
again break,
another case,
this time we say age is bigger than or equal to 365,
and age is smaller than 1095
that's the number of days in three years,
description, used break.
And finally,
case, age is larger than or equal to 1095.
So that means older than three years old.
Description equals old.
Break.
And then as I showed you in the example
the standard is to always provide a default expression
if none of these conditions are meant.
So that we at least get something
and even if we don't pass the right values
we say console log
and then I went to put in a template literal
and say, there is no description for
and then we put in the age.
All right.
Now we can save this and run the switch statement
and you see over here,
we are currently at 673 days
which on the status is used.
And looking at the logic that makes sense.
We can now test this very easily
by just setting age to a value.
So let's start by setting it to 29,
that should give us new, right?
29 days new,
so we're hitting the first condition.
Cool. What happens if I set it to 30,
lightly used that's the second condition, right?
Age is bigger than or equal to 30.
So let's set it to 301,
save still lightly used
because it's under 365.
So, 364
still lightly used
365 used.
Okay. So now we're in the third condition.
Then we can say,
let's say, 1094, 1094.
Save, still used 1095
and it's old.
Okay. So our switch statement works.
And as you can see we are using the switch statement
with logical statements
and combiners,
so we're testing for
two different logical statements at the same time.