Welcome back, ladies and gentlemen, welcome to new lecture about JavaScript conditionals.
Now, this is a very important lecture in make sure that you understand all of these conditions here.
If you don't just re watch the lecture, because this is the fundamental part of JavaScript.
So we're going to start with if statements, then we're going to go to else.
Then we're going to check what else means switch statements, ternary operator and logical operators.
These means and or and the last one, the exclamation mark means not OK, we don't have time to waste.
We have a lot of things I've created JavaScript lecture for and I'm illington here.
Just do way, not waste time.
OK, so write a comment here that we are going to do.
Let's put it in.
Was working mockable caps, Lock wasn't working for some reason, so JavaScript conditional.
Operators and we're going to say and logical operators very you now make here multiline comment.
Sorry.
And.
Here, what I'm going to right is Java script conditionals, and we're going to I'm going to rewrite
each and every of them if.
Alice.
Or else if switch.
And ternary operator, operator.
Very good.
Now here I will write about logical operators.
Yep, now here we will have and oops, and.
Or they will pop and the last one is not exclamation mark, OK, that is pretty much what we going to
cover in this lecture.
So let's start working on it.
Actually, here we can get rid of.
This doesn't look very nice.
Make sure your coat looks clean and nice.
OK, so that's what we are going to cover in today's lecture.
Let's be clear, a couple of variables here.
Number one, and that will be a variable number two.
And number two, let's put it 30.
And please don't make a space here.
Variable number three, and that will be value of 16.
So we have three variables declared and initialized with initial values no one will hold and number
two will hold 30.
And number three will hold 16.
OK, the first things first we need to explain what if condition means.
So if you say if condition.
Or if statement, it's pretty much the same.
So if statements and conditions means the same thing.
OK, now how we are going to write it if statement.
So the key word is, if so, this is the key word.
We always need to write it like that in inside here.
We need to have these brackets, OK?
Without these brackets, we don't have condition.
So inside these brackets, we are putting the condition in here.
I'm going to say no one is smaller than number two and curly brackets.
So make sure that you are following this this syntax.
So we have if then we have a bracket.
Oops.
And inside those brackets, we do set the condition and now the condition can be anything.
And I just put number one to be smaller then number two.
And that is the true number two.
Sturdee number one is eighty eight.
So here we can just type comes a lot, we can have a block of code here.
We can have anything but just to be very specific and short and to tell you that how if condition is
working, I'm just going to use Konza.
So don't think that only we can write comes a lot.
We can write multiple lines of code here based on our logic and in future you will see just that.
So here I'm going to say number two is bigger than number one.
OK, semicolon there.
And let's go back to our browser.
And here it is.
Number two is bigger than number one.
That is true because we saying if no if this condition is true, then.
This block of code needs to be executed, so this is how the if condition work, if statements or if
conditions are working.
So whatever the condition we set here, that needs to be true in order to go inside and run this code
so we can have multiple lines of code.
For example, we can say for ABC it will be to ten.
And if I can type it correctly.
Right.
So even I can do console that, lock that A, B, C if it is ten.
So we can have multiple lines of code, but they will be executed only if this condition is met.
When I say Matt means true.
Now the second part else.
Our statements, they're very important.
So how are we arriving, El's statement?
OK, so if no one is bigger now, I'm going to say then number two.
Number two, then what I'm going to say here, I'm going to change a little bit.
No one is bigger than number two.
I'm going to copy this one.
Just make sure that you copy everything in here.
I'm putting the else condition.
But what's most important for you to understand that we can't have here these brackets and set a new
condition here?
That is not how it works.
So let me just finish this one and I will explain you how it will be executed.
This means that number two is bigger than number one.
OK, let's talk about it now.
We do have the condition here and we say number one, which is waiting to be bigger than 30.
This condition will be not true.
So it will not go here and execute this block of code.
What will happen?
It will go straight to the alse condition and it will execute this line of code.
Let's see if that is going to work.
This means that number two is bigger than number one.
Very good.
So our logic is pretty much working because we just fixed the condition to our own needs.
We say, OK, this will be the condition.
And if it's not catched here, that means that number two is bigger than number one.
So the line of code.
So this is how it's usually been described.
So block of code will be executed only if this first condition fails, for example.
Now, if I changed back to this one, this here will be executed.
Number two is bigger than number one, right.
This will be executed and this will never be executed.
So when this condition is true, it will go here and then it will never go in the air.
So it will continue here.
So let me show you.
So it will skip the condition here outside of if else.
Condition right when they hear this here, outside of Al's condition, even if you put it here, you
see something here, it's not going to be catched because the first condition, if it's true, it will
go here and it will do it.
But let's change it like it was before.
So that is pretty much about if and else condition.
So let's check else if now, remember, I told you that here we cannot set a new condition, but with
ALS, if.
Yes, that is possible.
So I'm going to copy again this one and let's change a couple of things.
I'm going to leave this as it is so no one but I need to change here.
And I will say no one is bigger than number two.
Right.
OK, now what I want here is to have another condition where I'm going to say make sure that you put
the curly brackets here.
So inside this condition, I would say number one.
Is bigger than number three, right, and that is true, no one is bigger than number three because
18 is always bigger than 16.
So this here will be false.
This condition will return false and it will skip this one and it will go to the elusive condition.
Now, why it will go here, not there?
Well, because here we have another condition.
And so the block of coal will be executed if condition one fails and it's false.
So now the condition, too, needs to be checked.
OK, so yea I will see counsel that logged in.
I will ride.
This means no one is bigger than number three.
Good.
And we need to change the last part because if that is not true in our case, it is true here we can
say this means no one is the smallest of all.
Now here it will go and it will check.
It will return false.
This condition will never be executed, but it will go here and it will check No one.
It's true.
That is bigger than number three and it will print this line of code.
So it will never go to the ALS condition.
Let's save it and go back.
Very nice.
So our logic is working and now you know how to use if all else else if conditions.
So we shall see if we can put another condition.
For example, you want to check something extra that you haven't checked in the first one, then that's
it.
So these can be used to find which of the three numbers is the smallest.
Right.
So you can right here, this means that number three is the smallest of all.
And it will be true because number three is going to be checked.
Number one is bigger than number three.
Yeah, but we can you can have multiple multiple combinations here of if alse conditions.
Right.
OK, now, last part, not actually the last part.
Let's do another condition called switch.
Now we use switch to specify many alternative blocks of code to be executed.
So let's write how the if else conditions look like in real world world.
And after that, we are just going to type it back.
So for the here we do have if but here in switch, we need to use the key word switch.
Now inside we will have expression.
Now you know what expression means and we have case X column then code.
Block will go here, then break.
Let me just write it all, semicolon, then case.
Why in Code Block, then we have a break and the last one is called default.
And it will be called blog.
And break nice, so that is pretty much how this is written, so the switch expression is evaluated
only once.
So this expression can be anything and you will see, for example, we can put a number here, we can
put a condition here if we want inside, for example, five is greater than four, or we can just put
anything that we want.
And based on that expression, it's going to be evaluated at once.
So the value of this expression is compared to the value of each case and the value of each case is
going to be here.
So we need to put our logic there.
So if there is a match, for example, if I put X here and it will find there is X in this condition,
block will be match, and if it's match, then the associate block of code will be executed.
But we also need to have a break, because if we find a match, which is going to break from this,
which expression it will go out here.
So it will get out from this entire switch statement.
So if there is if there is no match, for example, if I put for example, I have X and Y, if I put
Z here, it will go through the first one.
There is no match this value.
It's not Z disvalue.
It's not Y.
Yeah, Y is not Z and it will go to the default.
It will run the default block and that's it.
It will get out in pretty much the speech expression is done.
OK, so that is basically how this switch works.
And this is pretty much the most complicated of all to understand.
But when somebody explain to you like this, it's not.
So let's create a variable here called DE in this program, we'll find the day of the week, for example,
if today's a Sunday, and that is actually two days of Sunday in my place, the day that I'm recording
this.
So we will find that today based on the switch statement, we will find which day of the week is.
But if you watch this video on Monday or Tuesday, you will have a different output.
So don't worry if you see Sunday with mine and if you see Monday or Tuesday or any day of the week in
your place.
Right.
So that doesn't mean that the CO is not working so far today.
And what I'm going to use for the first time is I'm going to use a date function here and this date
going to get day so we don't have to worry about this.
Now, what this basically will tell us will return is actual number.
So it will be zero to six.
I believe it will be zero till six.
So zero means Sunday.
And I will just show you.
So today.
So this entire method, it's inbuilt and we can use it right out of the box.
But you don't have to worry about this at this stage here.
So today's Sunday, that's why it's going to give me zero so zero in Sunday one.
It's Monday, Tuesday, three Wednesday for Thursday, five Friday and six Saturday now.
Because I know what are the numbers, I can build the cases so I can say here, I'm going to put the
date.
Now let's do the switch.
And inside expression, I can just create this variable.
I can just put this variable.
And I know that this variable will return a number either will be one zero one two three six doesn't
matter.
So it will return us a number so I can build these cases now.
So everything will be easy.
Now, let's say case zero and I will say de equal sun.
Very good.
Then we will have the break then another case, case one.
So in case today was Monday, it will go here.
In this case in what I have a block of code there.
I will say de equal to Tuesday on Monday.
Sorry.
And break, then I'm going to copy and paste it now, look what I have done here just to clear the variable
day, but I've never initialized to anything you can do that it will be rewritten here because you're
going to put a new value there.
So I haven't tell you this.
For example, let's do it like this.
If you put something here.
Right.
And if you cancel it, log.
If you consulate log today day, but let me just first comment about this part now.
I have something in store in the day variable, right, but that will change because in a switch statement,
I'm declaring a new variable for the day variable.
So I'm actually I'm declaring a new value for the day variable.
Right.
So whatever we have here, it will be overridden.
OK, now let's do the rest of the blocks.
So I'm just going to copy them.
And I knew that it will mess it up a little bit.
Right.
OK, that's why I don't want to copy and paste too much, but so zero one it will be too.
This will be three and Tuesday.
And this will be Wednesday, so you can you can use anything you want.
You can say short Wednesday.
Now, let's face this one.
A couple more times here, so case zero, one, two, three, then.
Uh, this will be for this will be Phibes five and this will be six, and we are done, ladies and gentlemen,
so four will be Thursday.
And number five will be Friday, number six will be.
Saturday.
And you can put it here, default, so if there is a some number, but it will not happen, but if you
put here manually seven, we are not getting anything seven.
So we need to specify our default code block here.
So the default statement will say they equal something went wrong.
And Rick, that is pretty much it.
So.
What is the last thing we need to do to control it, like the day of the week?
Nice.
OK, so let's go through this statement one more so the Swedish statement, we said the expression here
and this expression, this function will return from zero until six.
And because of that, we build the cases zero until six.
And if today's zero, it will go here, it will match this value.
So make sure that you are you understanding this part.
Whatever we put here, if we put high here, we need to have high here as well in order to get in this
part before in the past, when I was learning this while I was a little bit confused because no one
told me so, they just gave us exercises to do.
And I was just confused why how is this working?
But now, you know, so based on this, the value of this expression, it will be matched in and in
one of these condition.
If not, the default will kick in.
OK, and inside we setting the date to before was something.
Now we sitting to Sunday, Monday, Tuesday, Wednesday and so on, so forth.
OK, save it.
Now, let's go back here and it's going to say Sunday because today is really Sunday.
Very good.
And I'm not I don't need to control the clock here.
So that is pretty much about the switch statement.
The last thing that I want to explain is ternary operator.
Now, this is a basically pretty much new but I just want to include it here.
So for the ternary operator, we have something like this condition.
Then we have a question mark, then we have expression.
If true, then we have Callon here, an expression if false.
And this is pretty much confusing, right?
So this operator is frequently used as a short cut of this weight of this if statement, if else.
Right.
So we have a condition here.
We're saying if this condition is true, then do this block of code else do this block of code.
Well.
That is pretty much it, but we are going to do in one single line, we are setting the conditions first.
The question mark, if the condition is true, Will will go to this one.
And this column here means if the condition is false, whatever we put here, it will be executed.
Well, let's, for example, set some person age to be twenty six and check if he can drink beer or
juice based on his age.
So I'm going to say var variable my age and let's put it here.
Twenty six.
You can always practice this with prompt, you can prompt the user for age and.
Yeah but today I don't have time to do this.
Now I'm going to say beverage and here I'm going to set the condition actually.
I'm doing this.
Let me just show you first so we can do it like this, my age is greater or equal to twenty six, but
it will work with greater only.
Just want to be sure.
Then the question mark, I can have a beer and else I will have juice.
And that's pretty much it.
Now, the output of this, I just want to store it in a variable called beverage and what will happen.
So my age is 26.
It will go here and it will say, OK, here should be twenty one.
Right to always be.
So twenty six.
So I'm saying twenty six is bigger or equal to twenty one.
It's bigger, it's not equal but it's bigger than twenty one because that condition is true.
We are going to do this part and this part is the if condition.
Right.
If that was not true, I will only be allowed to drink juice.
So beverage will hold beer, not juice.
Because this condition is true and we need true.
We have this expression then if we have something else then we're going to have false conditions.
OK, now control it like the beverage.
Save it.
Here he is.
I do have a beer.
What if what will happen if I have 20 years?
Now, the condition is false and expression is false will be this after this column here.
And I will have juice because a minor I can't drink now.
That is called ternary operator and we usually use this operator as a short version of if our statement
that is pretty much it.
Now, last part, we will do logical operators
and we're going to start wi and now this is a very, very nice.
In order for you to understand, I'm going to say our first first name and that will be Tom, Tom and
Vau last name.
Let me just write this one cruise.
I hope I spelled his last name correctly, if a first name is equal, equal to Hoopes, this means equal
in to Tom.
And last name is equal, equal to Crewes.
And let me just write this one down and we can just talk it, what will happen while we have a famous
by the way, you can write anything you want to hear.
Actor.
Here.
Doing JavaScript.
Nice.
OK.
Ls.
Council that long form.
Get back here and start working.
It is an exclamation mark because we are yelling at him.
That's not good, right?
We not like that, we're not yelling to anyone.
OK, now pretty much this may be a confusing so what we did.
So we create two variables.
First one is the first name it will hold value to.
The second one is the last name.
It will hold value of cruise hits its last name right now what we are doing here.
So we are using logical operators in combination with the JavaScript operator.
Yeah, we are using in combination with JavaScript conditionals with any of this right now why we are
doing this.
So when I have these and means what I have in the first condition needs to be true and what I have in
the second condition needs to be true in order to execute this code of block.
So both of these cases here, the first one, the one that I have on the left from the end operator
needs to be true.
Right?
So that is how it's working.
And in our case, our first name is true for Tom and our last name is true.
It's Kroos.
So it should go here and say, wow, we have a famous actor.
That's pretty much it.
Now, if I, for example, say here that the first name should be Tommy and the last name should be.
Well, this is not true.
The first name holds Tom and this condition will be false.
So I have false and true and false and true will always get me back false.
So it will not go here.
It will be catched in the condition.
Tom, get back here and start working.
So this is how the end operator works.
Both sides of this line here needs to be true in order to order this code or block to be executed.
OK, now I'm going to copy the same thing again.
And, uh, let's change this one to order.
And I'm not going to redeclared him again.
Right.
That's not a good point in here.
Change the end with or now.
If I do it like that, I will have the same same output.
Why?
Because war means only one of the condition doesn't matter if this or this needs to be true in order
for this block to be executed.
It's only one.
For example, if I put here Tommy, that is false.
But this one is true.
So other means just I need any of this condition to be true in order to get here.
And yet we do have the same result.
So any only one, only one scenario is possible to go to the condition when this is both of the conditions
here.
Are we doing we are checking.
They are not true.
And that will go back there.
So Tommy and Cruz, they're not same what we have stored here in this variable now, you know, the
power of.
And I'm going to leave it like this with one mistake here, because you need to know, so that is pretty
much it.
Now, I can see here two of the conditions.
Only one of the conditions can be only one of the conditions needs to be true in order to execute the
if block.
Right.
And I'm going to copy the same thing in here.
It's different to the condition to two of the condition needs to be true in order to execute the IV
like cool.
And the last one is just going to copy this one again and let's change it.
This should be exclamation mark.
End means not.
OK, so what not does pretty much interesting, so let's I don't need here to conditions for this one
to explain it and we're going to do more in future.
So don't worry about it, so we're going to do more in future with not so you're going to have more
practice and if you don't understand it right now, you just don't need to freak out.
That's what I'm trying to say.
OK, now I'm going to say it like this, and I'm just going to cut it from here.
I'm going to say exclamation mark.
I'm going to put another pair of brackets, put the condition there.
So first name equal to Tom.
Let's change this one.
And I will say Conjola.
Hi, Tom.
Make sure that high capital H looks doesn't look good in here.
Who are you?
If you're not Tom, then who are you in questionmark.
Right.
So what do you think will happen?
Let's go back here.
Who are you?
Well.
Let me just copy this one first name here just to explain this one.
So first name?
Well, first name, it's Tom and it's equal to Tom.
So what is happening?
Well, we have this not and what this operate what this operator logical operator would do.
It will do will turn true condition to false and false to true.
Now because I have it here and this entire thing doesn't matter with the operator needs to be true in
order to get here.
But the first part was true and say the first name is pretty much equal to the value system.
But here I just say Nannygate make that opposite.
So if it's true, make it false.
So if this condition is false, then this will never be executed.
It will go here.
So make sure that you understand this part if we have here, Tommy.
Now, look, how bizarre is this.
So if we have here, Tommy, and this is not true, but here with the the not operator will say, yeah,
now the false I will make it true.
If if true, then I will do this part.
Say hi, Tom.
Right.
So make sure that you understand the not operator will turn every anything that we have here to the
opposite value.
If it's true, it will turn into false.
If it's false, it will turn out to true.
And that is pretty much it.
We will do much, much more.
And you have exercises.
Please do them.
So let me just recap what we have done.
We have done all of these conditionals and we have included these logical operators.
And yeah, that is pretty much it for the if to be this code of block to be executed, this condition
should be true.
And the keyword is if now when we have else, when this condition is not true, the else kicks in and
the code of lock here that we have will be executed.
The else if if we use or else if we always use in a combination with if y because this condition, if
this condition is not true, it will go to check the second condition that we said.
And even if that is not true, it will go to the LS.
But if it's true, it will print out here.
The switch statement requires an expression.
Wherever we have here in this expression needs to be found it in the cases so case X and Y so needs
to be matched.
And that's why we did here.
They but I'm just going to delete here.
I'm just going to leave it like they just like that.
It is Sunday.
Now this is working because here we just declaring the value based on the expressions.
If the expression is zero it will go here at zero and the function here and you date that get they will
always return from zero till six.
And that is why we can just create this switch statement and find out what day of the week we are at
the moment.
So in your when you watching this video, if you watching on Tuesday, you will have a different you
will have two and then you will need to probably the output will be Tuesday here.
So don't be afraid.
If you don't see Sunday as me, that's pretty much it for this weekend.
And we did the ternary operator.
Are we going to do this operator in future semicolon here?
I did forget that part and make sure that you're not doing the mistakes that I do.
OK, I will see this one.
This was super long and I'm so sorry about this.