Welcome back to Section three, lesson seven, Operator Precedent's.
So this is a little bit of a follow up to the operators discussion we just had, but we're going to
learn a little bit more about how competitors come before arithmetic, unless you use parentheses and
just the general order of operations in JavaScript.
So you might have learned about order of operations.
If you learned about mathematics in school, the parentheses would come before exponents for multiplication,
division, addition and subtraction.
And there are slightly different orders of operations in different math classes around the world.
Now, since there are more operations in JavaScript, we have to have in order for them.
So they understood in the right order.
And this will also review that X equals X times.
Y is the same as X times equals Y.
So that's the arithmetic operators we learned last time.
And this variable plus plus and variable minus minus is the same as plus plus variable and minus minus
variable.
Except that in sequence this would get returned first and then the operation would happen to increment.
This would get returned first, then the operation to decrement.
Likewise this would but Symbicort to contrast this would get added first and then the final variable,
the changed variable would get returned.
This would get reduced first decrement in first and then the variable would get returned.
So let's take a look at this example and then we're actually going to put it into our code.
Let's say we have variable now is twenty twenty or we'll do twenty twenty one and then fight a year.
Let's say it's twenty eight for age would be would be fifteen while it's let's make it thirteen for
our example is full H is now minus five year less than or equal to the full H.
True.
Is the of age now minus fight a year and then Whiskas, age nine, to be a different animal?
Let's take the average of the two, so let's add these things together and divide by two.
So notice what's happening in these examples right here.
We're using minus and also a comparison less than or equal to and also parentheses.
So there's three different types of operators happening here.
Same thing here, three different types of operators.
You've got parentheses.
We have plus and we have division, OK, and then multiple assignments declare these variables X and
Y and then you can go ahead and assign X to the value Y at the same time you can assign the value of
here.
So let's just break it all down for you.
OK, let's go over and do this in our code.
OK, so we're here, we've got our blank slate.
And let's start with this.
And just as a review, this is the operator precedence.
So you can just go to this page from the resources.
This will be in the media and Web docs on Mozilla.
And so this table is about halfway down and this tells you the order that it's going to read those operations
in.
So it's going to start with grouping.
That's the parentheses.
And then it will do any this these ellipses.
This is for the spread operator and then it will do also the new operation we can get into in the future.
And then it will also then it will do these right here.
The logic will not bitwise unary type of and then it will get into the arithmetic operations, exponentiation,
multiplication, division, subtraction, remainder, addition, subtraction.
So this, as you can tell, is in the same the same order as a traditional math operations.
And then there are others down here.
You have the comparison operators for greater than equal to cetera, and then you get into logical and
in or in some other conditionals and then you get into the assignment operators and you saw a few others
in this table that we didn't go over.
OK.
So let's get into the example, so let's start now with let now equal 20.
Twenty one, and then let's do let Feydeau year equal twenty eight.
OK.
And let's let full age.
So this is how old the dog is going to get to 15.
OK, so is he 15 yet if he was born in two eight?
Well, no, we can just we're just telling that by just looking at it.
So let's actually do the math.
So let's do let is full age equal.
Now we're putting this in parentheses minus phyto year.
OK?
So we could start with that, and that would be just two thousand twenty one, minus two thousand eight.
That would be 13 years.
This is asking, is he the full age yet?
So we're going to say, is it true that he's less than or equal to the full age?
Or I guess if we wanted to ask, is he full age, then we'd say, is that greater than or equal to the
full age?
And so that would be false.
OK, let's go ahead and see if we get that correctly.
OK, so let's go ahead and console dialogue.
Is full age.
Let's see what happens.
OK, false.
Now, if we said, is he less than the full age?
Is not full age.
OK.
And then we asked if we could look at that, that would be true.
OK, let's double check it.
True, there we go, so all we did is we changed this greater than to a less then greater than or equal
to to a less than or equal to.
And we found out that two thousand twenty one minus two thousand eight, which is 13, is less than
15.
So since thirteens, less than 15, that's true.
And so true.
Gets stored in is not for age.
OK.
Just to double check all these numbers, because now.
Here, let's do this, do all of these, OK?
So let's do.
This will be now.
This we fight a year or
so before age
and is not for age.
OK.
So let's say that it again, so you can see we have those same values here inside JavaScript and here's
what happened.
Twenty, twenty one minus twenty eight, that's 13.
That is less than or equal to 15.
If we go back to our other example, is it the full age already is a greater than or equal to is thirteen
greater than or equal to fifteen.
No, it's not.
So that would be false.
That's false.
OK, so that's what we get.
All right, so what if we go further, if we want to go down here and let's take these council logs
out?
We're just going to comment them out, remember, they're a little trick, you can just do a control
forward slash or command forward slash on Mach.
And that way, you can comment out multiple lines.
OK, now let's go further, let's check and see, let Fido's age equal now minus phyto year.
So let's just copy that down so we don't have to use that.
We can basically just use the variable.
All right, let's do.
Now, let's add Whiskas each, so whiskers is going to be a cat and we're going to start that out at
nine years old.
Now, let's get the average equal.
Phyto H.
Plus Whiskas, each divided by two, because that's how many items that we have.
Do we get the average?
So let's consider log that average and see what it is.
OK, save and run that it's 11 points, the average of 15 or rather it was 13, right?
Twenty one minus twenty.
Twenty eight is thirteen years.
Thirteen and nine averaged together.
That's eleven because you take thirteen plus nine.
That's twenty to twenty two.
Divided by two is eleven.
OK, now let's do another example.
Show you some operator precedence.
We'll get into some more math and just show you let X and Y.
OK, so we've got X and Y and let's just show you that you can actually do this now this year you can
see this very often out in the wild in JavaScript, but you can totally do this.
You can do six plus two times five minus seven.
OK, and what is this going to be eight you're going to solve for the parentheses first?
OK, so that's going to be eight.
And that's going to be eight times five, so eight times five is going to be.
Eight times five, and then it's minus seven after that.
OK, so that's the same as 40 minus seven.
And so that's going to be thirty three.
All right.
So we just did that in our head.
What if we want to just console log X and Y just so we can see what those values are?
OK, let's just consider log X, comma Y, and that will console log both of them side by side.
OK, try that one.
So we don't have to deal with Fido and Whiskers again.
Thirty three and thirty three.
So what it did is it did the order of operations to solve for these values here and stored that in Y
and then it also tied X as a shortcut to the same data that Y is tied to as a shortcut.
So those variables have the same value because they use the assignment operation to say R, X equals
Y, so Y equals X, OK, and then just as a review.
We'll just show you that.
If we change, X equals X times five.
That's the same as doing X times equals five.
All right, so we'll just show you what the console log is after each of those cases,
we know it's thirty three, so let's take thirty three times five.
That would be approximately one hundred and sixty five.
So that's the scene right there.
One hundred sixty five.
And now let's just move it down here.
And let's hide these lines, OK, so it's only going to use this and multiply that by five and we should
get one sixty five still.
We still get one sixty five and how do we know that we're not just seeing our old code?
We'll do one time.
We'll do that time six.
There you go.
See it added another thirty three.
OK, so those are the same X equals X times five and X times equals five, so let's go ahead and wrap
it up.
But that is operator precedence.
So showing you the order of operations for JavaScript that you're going to have to solve parentheses
first.
A few other things first, then you'll do the arithmetic operations and then you'll do.
The logical operations and then so on from there.
OK, see you in the next video.