Up to this point we learned the 'if' statement, the 'if else' statement and the 'else if' statement
And if you remember our section outline, well we haven't mentioned these two for javascript conditionals.
So we're going to tackle those today.
As you can see we have two more to get through but don't be scared.
These are not going to introduce anything new.
There are just different ways of doing control flow but they won't do anything that a simple if else
statement can't do.
However there are cases where it will be much easier to do these two then if we do something with the
'if else' statement.
So let's check them out.
The first thing we're going to talk about is the 'ternary operator'.
It looks something like this,
'condition ?'
expression one (expr1) or expression two (expr2).
And that is the syntax. It says, Is this true or false?
If it's true then provide this value (expr1).
If it's false provide this value(expr2).
Let me show you an example of this, will have a function called 'isUserValid' that accepts a boolean (bool)
and will just return the boolean value.
Now within here we can say variable answer equals 'isUserValid'
and we'll say 'true' for now question mark(?)
So if that's true I'll say 'You may enter'
otherwise I'll say 'Access denied'
Let me make this a little bit smaller so you can see.
So again all we're saying is if this is true then do this expression("You may enter")
If this is false do this expression("Access Denied").
So just copy this into the console and see what happens.
If I run 'anwer', Whoops I spell that wrong, 'answer'.
I get, 'You may enter' because
user is valid (isUserValid) is true.
Now if I changed this to say 'false', well in that case I'll get 'Access denied'
Let's do a little bit more of a complicated answer.
We can say variable (var) 'automatedAnswer' and we can say and I'll do this on a new line just so you can see
'Your account number is', 'isUserValid' and we'll just say something like.
'false' for now.
So because we have these brackets we're gonna have an expression inside of here that we can evaluate
so we can say is user (isUserValid) is valid.
If the user is valid will say '1234'.
If not, we'll say 'not available'.
If we copy and paste this here, into our console and we can see it better.
We see, if user is valid (true)
We want to say '1234'.
If it's not then 'not available'.
So if I run this and now I look at automated answer, I get 'Your account number is not available' well,
because this user value is 'false'.
But if I do 'true' well in that case your account number is '1234'.
Now you might notice that this is actually not that different from if you did,
'function condition' was called a condition and will say
'if isUserValid' return.
'You may enter'
and you did 'else'
'return Access denied' this function over here, 'condition', if I say variable(var) 'answer2' equals 'condition'.
Let's see what happens.
Copy these two, run them, and now if I look at the 'answer' I get 'You may enter' if I get the 'answer2'
I get 'You may enter'.
Now it's the exact same thing other than well bit of mis-spelling on my part.
But you see that one way is a lot nicer, It's just nice simple one line. A ternary operator is really really
good for these 'if else' where there is a check for a condition and there's two possible expressions,
that come out of it.
All right let's learn, the next one, which is the 'switch' statement.
This is the fun one.
So a s'witch' statement looks something like this.
And we can build a simple game right now to demonstrate how it works.
I can say function 'moveCommand'.
And we'll say it accepts parameter 'direction' within this function.
I'll say variable(var) 'whatHappens' and we'll say 'switch'.
Notice that it's a key word.
So it's now highlighted in red.
And I'll say 'direction'.
The 'switch' statement is gonna accept 'direction' it's going to check 'direction'.
And within here we'll say 'case' 'forward' which means check 'direction' if 'direction' equals 'forward',
Then 'whatHappens' equals
'You encounter a monster;'
'break;'.
I'll explain more 'break' does right after I finish up the rest of the case statements.
So case statements as you can see are really good because I can just copy and paste.
And now I can say for here we'll say 'back' and if direction is 'back' we'll say 'You arrived home' if the
direction is 'right' in that case we'll say 'You found a river' and if the 'direction' is 'left' we'll say
'You run into a troll'.
And then finally we'll say 'default'
'whatHappens' equals to 'please enter a valid direction'.
And we'll end it with a 'return' 'whatHappens'.
All right.
That was a whole lot of code.
Let's run this command first and then we'll explain exactly what is happening.
Copy the code.
Copy into the console.
And now we have the 'moveCommand'
And if you remember the last video 'moveCommand' now exists in the root scope which is window,
so within here.
Let's just say 'forward', now if I run this I get, 'You encountered a monster'.
If I go 'back', 'You arrived home'.
If I go 'right', 'You found a river'.
If I go 'left', 'You run into a troll' and if I write jibberish
I get.
'Please enter a valid direction'.
That's what switch statements are really good for when you have a lot of conditions.
Instead of using an 'if else', 'if else', 'if else', 'if else', 'if else' statement, you can use a 'switch' statement
and you can see, it reads nicely.
Essentially what it's saying is create variable 'whatHappens' then switch.
I want you to check whatever the condition is, whatever the variable is, in this case that's the direction.
And in case, the direction equals 'forward' do this, in case direction is 'back' do this.
If it's 'right' do this, if it's 'left' do this.
And it says 'default' is if none of these conditions work and remember the program goes line by line
so it checks 'forward' then it checks 'back' and it checks 'right' and the checks 'left'.
And if nothing of those match then it says you can just default to 'please enter a valid direction' and
then we return 'whatHappenes' so that was so that the function returns something for us.
Now finally what is the 'break' well the 'break'(s) simply says,
If 'case' is 'forward' then assign 'you encounter a monster' to
'whatHhappens' variable and then 'break', 'break' is just saying break out of the switch statement.
So instead of the program then checking to see if 'case' is 'back', 'case' is 'right', 'case' is 'left' or 'default'.
If we say 'break', the program stops here and then goes straight out of the switch statement
to return 'whatHappens'.
So if I actually do, 'break' before 'whatHappens', 'you encounter a monster' and I copy this
and let's rerun the function, we'll say 'moveCommand' is 'forward' notice what happens now.
I get 'undefined' because we broke the program enters saying, Oh yeah!
He typed in 'forward' but before we're able to assign something to the variable I say 'break',
I exit out of the switch statement and I return 'whatHappens' which it's only been declared but it's unassigned.
So I just return 'undefined'.
All right there you have it, your two other conditionals and you've covered all of them in javascript.
Now each one is good for its own individual case but they're all very useful.
I'll see you in the next one Buh-bye.