1
00:00:00,510 --> 00:00:04,680
Welcome back to Section three, lesson seven, Operator Precedent's.

2
00:00:05,220 --> 00:00:09,600
So this is a little bit of a follow up to the operators discussion we just had, but we're going to

3
00:00:09,600 --> 00:00:15,900
learn a little bit more about how competitors come before arithmetic, unless you use parentheses and

4
00:00:15,900 --> 00:00:20,780
just the general order of operations in JavaScript.

5
00:00:20,820 --> 00:00:23,220
So you might have learned about order of operations.

6
00:00:23,220 --> 00:00:28,950
If you learned about mathematics in school, the parentheses would come before exponents for multiplication,

7
00:00:28,950 --> 00:00:31,050
division, addition and subtraction.

8
00:00:31,050 --> 00:00:35,600
And there are slightly different orders of operations in different math classes around the world.

9
00:00:36,390 --> 00:00:41,010
Now, since there are more operations in JavaScript, we have to have in order for them.

10
00:00:41,010 --> 00:00:43,850
So they understood in the right order.

11
00:00:43,860 --> 00:00:47,850
And this will also review that X equals X times.

12
00:00:47,850 --> 00:00:50,000
Y is the same as X times equals Y.

13
00:00:50,010 --> 00:00:52,740
So that's the arithmetic operators we learned last time.

14
00:00:53,520 --> 00:00:59,670
And this variable plus plus and variable minus minus is the same as plus plus variable and minus minus

15
00:00:59,670 --> 00:01:00,060
variable.

16
00:01:00,060 --> 00:01:08,430
Except that in sequence this would get returned first and then the operation would happen to increment.

17
00:01:08,880 --> 00:01:12,060
This would get returned first, then the operation to decrement.

18
00:01:12,990 --> 00:01:20,070
Likewise this would but Symbicort to contrast this would get added first and then the final variable,

19
00:01:20,070 --> 00:01:22,590
the changed variable would get returned.

20
00:01:22,980 --> 00:01:28,230
This would get reduced first decrement in first and then the variable would get returned.

21
00:01:28,540 --> 00:01:31,620
So let's take a look at this example and then we're actually going to put it into our code.

22
00:01:32,430 --> 00:01:39,510
Let's say we have variable now is twenty twenty or we'll do twenty twenty one and then fight a year.

23
00:01:39,510 --> 00:01:46,960
Let's say it's twenty eight for age would be would be fifteen while it's let's make it thirteen for

24
00:01:46,960 --> 00:01:53,490
our example is full H is now minus five year less than or equal to the full H.

25
00:01:53,670 --> 00:01:54,180
True.

26
00:01:55,120 --> 00:02:02,550
Is the of age now minus fight a year and then Whiskas, age nine, to be a different animal?

27
00:02:02,830 --> 00:02:07,200
Let's take the average of the two, so let's add these things together and divide by two.

28
00:02:07,210 --> 00:02:10,710
So notice what's happening in these examples right here.

29
00:02:10,720 --> 00:02:17,700
We're using minus and also a comparison less than or equal to and also parentheses.

30
00:02:17,740 --> 00:02:20,730
So there's three different types of operators happening here.

31
00:02:21,760 --> 00:02:23,920
Same thing here, three different types of operators.

32
00:02:23,920 --> 00:02:24,880
You've got parentheses.

33
00:02:24,880 --> 00:02:32,350
We have plus and we have division, OK, and then multiple assignments declare these variables X and

34
00:02:32,350 --> 00:02:40,360
Y and then you can go ahead and assign X to the value Y at the same time you can assign the value of

35
00:02:40,360 --> 00:02:40,740
here.

36
00:02:41,470 --> 00:02:43,690
So let's just break it all down for you.

37
00:02:44,200 --> 00:02:46,420
OK, let's go over and do this in our code.

38
00:02:48,460 --> 00:02:50,680
OK, so we're here, we've got our blank slate.

39
00:02:51,340 --> 00:02:52,390
And let's start with this.

40
00:02:52,750 --> 00:02:55,390
And just as a review, this is the operator precedence.

41
00:02:55,390 --> 00:02:58,400
So you can just go to this page from the resources.

42
00:02:58,420 --> 00:03:02,070
This will be in the media and Web docs on Mozilla.

43
00:03:02,920 --> 00:03:09,820
And so this table is about halfway down and this tells you the order that it's going to read those operations

44
00:03:09,820 --> 00:03:10,230
in.

45
00:03:10,240 --> 00:03:11,950
So it's going to start with grouping.

46
00:03:12,460 --> 00:03:13,930
That's the parentheses.

47
00:03:14,470 --> 00:03:17,550
And then it will do any this these ellipses.

48
00:03:17,560 --> 00:03:24,700
This is for the spread operator and then it will do also the new operation we can get into in the future.

49
00:03:25,540 --> 00:03:28,990
And then it will also then it will do these right here.

50
00:03:29,020 --> 00:03:38,590
The logic will not bitwise unary type of and then it will get into the arithmetic operations, exponentiation,

51
00:03:38,590 --> 00:03:43,810
multiplication, division, subtraction, remainder, addition, subtraction.

52
00:03:43,820 --> 00:03:49,480
So this, as you can tell, is in the same the same order as a traditional math operations.

53
00:03:50,170 --> 00:03:51,640
And then there are others down here.

54
00:03:51,640 --> 00:03:57,280
You have the comparison operators for greater than equal to cetera, and then you get into logical and

55
00:03:57,280 --> 00:04:03,970
in or in some other conditionals and then you get into the assignment operators and you saw a few others

56
00:04:03,970 --> 00:04:05,410
in this table that we didn't go over.

57
00:04:05,840 --> 00:04:06,190
OK.

58
00:04:07,130 --> 00:04:17,330
So let's get into the example, so let's start now with let now equal 20.

59
00:04:19,330 --> 00:04:29,770
Twenty one, and then let's do let Feydeau year equal twenty eight.

60
00:04:30,670 --> 00:04:31,110
OK.

61
00:04:35,110 --> 00:04:36,850
And let's let full age.

62
00:04:37,120 --> 00:04:42,010
So this is how old the dog is going to get to 15.

63
00:04:42,390 --> 00:04:47,080
OK, so is he 15 yet if he was born in two eight?

64
00:04:47,440 --> 00:04:51,250
Well, no, we can just we're just telling that by just looking at it.

65
00:04:51,260 --> 00:04:52,630
So let's actually do the math.

66
00:04:54,230 --> 00:05:01,190
So let's do let is full age equal.

67
00:05:04,430 --> 00:05:12,050
Now we're putting this in parentheses minus phyto year.

68
00:05:12,710 --> 00:05:13,180
OK?

69
00:05:15,030 --> 00:05:20,980
So we could start with that, and that would be just two thousand twenty one, minus two thousand eight.

70
00:05:21,000 --> 00:05:23,410
That would be 13 years.

71
00:05:23,850 --> 00:05:27,410
This is asking, is he the full age yet?

72
00:05:27,990 --> 00:05:33,390
So we're going to say, is it true that he's less than or equal to the full age?

73
00:05:37,120 --> 00:05:42,220
Or I guess if we wanted to ask, is he full age, then we'd say, is that greater than or equal to the

74
00:05:42,220 --> 00:05:42,850
full age?

75
00:05:44,110 --> 00:05:45,430
And so that would be false.

76
00:05:46,540 --> 00:05:50,450
OK, let's go ahead and see if we get that correctly.

77
00:05:50,470 --> 00:05:52,840
OK, so let's go ahead and console dialogue.

78
00:05:55,690 --> 00:05:56,590
Is full age.

79
00:05:58,060 --> 00:05:59,010
Let's see what happens.

80
00:06:01,950 --> 00:06:02,890
OK, false.

81
00:06:03,840 --> 00:06:06,690
Now, if we said, is he less than the full age?

82
00:06:07,950 --> 00:06:09,610
Is not full age.

83
00:06:10,010 --> 00:06:10,500
OK.

84
00:06:12,000 --> 00:06:16,200
And then we asked if we could look at that, that would be true.

85
00:06:17,160 --> 00:06:18,590
OK, let's double check it.

86
00:06:20,560 --> 00:06:26,350
True, there we go, so all we did is we changed this greater than to a less then greater than or equal

87
00:06:26,350 --> 00:06:28,210
to to a less than or equal to.

88
00:06:28,810 --> 00:06:35,320
And we found out that two thousand twenty one minus two thousand eight, which is 13, is less than

89
00:06:35,920 --> 00:06:36,630
15.

90
00:06:36,940 --> 00:06:40,570
So since thirteens, less than 15, that's true.

91
00:06:41,140 --> 00:06:41,950
And so true.

92
00:06:41,950 --> 00:06:44,780
Gets stored in is not for age.

93
00:06:45,220 --> 00:06:45,590
OK.

94
00:06:46,820 --> 00:06:50,060
Just to double check all these numbers, because now.

95
00:06:51,340 --> 00:06:53,610
Here, let's do this, do all of these, OK?

96
00:06:55,000 --> 00:06:56,050
So let's do.

97
00:07:00,030 --> 00:07:01,080
This will be now.

98
00:07:04,630 --> 00:07:07,120
This we fight a year or

99
00:07:12,420 --> 00:07:13,440
so before age

100
00:07:16,200 --> 00:07:17,450
and is not for age.

101
00:07:17,670 --> 00:07:18,020
OK.

102
00:07:18,920 --> 00:07:27,590
So let's say that it again, so you can see we have those same values here inside JavaScript and here's

103
00:07:27,590 --> 00:07:28,180
what happened.

104
00:07:28,640 --> 00:07:31,400
Twenty, twenty one minus twenty eight, that's 13.

105
00:07:31,830 --> 00:07:34,250
That is less than or equal to 15.

106
00:07:35,390 --> 00:07:43,340
If we go back to our other example, is it the full age already is a greater than or equal to is thirteen

107
00:07:43,340 --> 00:07:44,780
greater than or equal to fifteen.

108
00:07:44,840 --> 00:07:45,980
No, it's not.

109
00:07:47,000 --> 00:07:48,830
So that would be false.

110
00:07:51,620 --> 00:07:52,370
That's false.

111
00:07:54,950 --> 00:07:56,510
OK, so that's what we get.

112
00:07:58,300 --> 00:08:05,650
All right, so what if we go further, if we want to go down here and let's take these council logs

113
00:08:05,650 --> 00:08:05,920
out?

114
00:08:07,240 --> 00:08:10,750
We're just going to comment them out, remember, they're a little trick, you can just do a control

115
00:08:10,750 --> 00:08:14,410
forward slash or command forward slash on Mach.

116
00:08:15,700 --> 00:08:18,130
And that way, you can comment out multiple lines.

117
00:08:20,330 --> 00:08:32,640
OK, now let's go further, let's check and see, let Fido's age equal now minus phyto year.

118
00:08:33,170 --> 00:08:38,870
So let's just copy that down so we don't have to use that.

119
00:08:38,870 --> 00:08:41,840
We can basically just use the variable.

120
00:08:44,670 --> 00:08:45,870
All right, let's do.

121
00:08:47,420 --> 00:08:56,540
Now, let's add Whiskas each, so whiskers is going to be a cat and we're going to start that out at

122
00:08:56,810 --> 00:08:57,560
nine years old.

123
00:09:00,210 --> 00:09:03,840
Now, let's get the average equal.

124
00:09:06,920 --> 00:09:07,760
Phyto H.

125
00:09:10,100 --> 00:09:16,250
Plus Whiskas, each divided by two, because that's how many items that we have.

126
00:09:17,610 --> 00:09:18,660
Do we get the average?

127
00:09:19,710 --> 00:09:22,140
So let's consider log that average and see what it is.

128
00:09:27,560 --> 00:09:37,970
OK, save and run that it's 11 points, the average of 15 or rather it was 13, right?

129
00:09:38,900 --> 00:09:40,490
Twenty one minus twenty.

130
00:09:40,490 --> 00:09:42,470
Twenty eight is thirteen years.

131
00:09:43,340 --> 00:09:45,520
Thirteen and nine averaged together.

132
00:09:45,560 --> 00:09:49,970
That's eleven because you take thirteen plus nine.

133
00:09:50,180 --> 00:09:51,830
That's twenty to twenty two.

134
00:09:51,830 --> 00:09:53,510
Divided by two is eleven.

135
00:09:53,990 --> 00:09:56,180
OK, now let's do another example.

136
00:09:56,840 --> 00:09:58,520
Show you some operator precedence.

137
00:09:58,520 --> 00:10:05,030
We'll get into some more math and just show you let X and Y.

138
00:10:07,310 --> 00:10:13,880
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

139
00:10:13,880 --> 00:10:17,540
see this very often out in the wild in JavaScript, but you can totally do this.

140
00:10:18,560 --> 00:10:28,700
You can do six plus two times five minus seven.

141
00:10:33,100 --> 00:10:38,650
OK, and what is this going to be eight you're going to solve for the parentheses first?

142
00:10:39,550 --> 00:10:40,900
OK, so that's going to be eight.

143
00:10:45,480 --> 00:10:50,340
And that's going to be eight times five, so eight times five is going to be.

144
00:10:53,520 --> 00:10:57,180
Eight times five, and then it's minus seven after that.

145
00:10:57,660 --> 00:11:02,400
OK, so that's the same as 40 minus seven.

146
00:11:04,140 --> 00:11:05,880
And so that's going to be thirty three.

147
00:11:06,240 --> 00:11:06,530
All right.

148
00:11:06,570 --> 00:11:07,830
So we just did that in our head.

149
00:11:07,860 --> 00:11:13,170
What if we want to just console log X and Y just so we can see what those values are?

150
00:11:13,800 --> 00:11:23,790
OK, let's just consider log X, comma Y, and that will console log both of them side by side.

151
00:11:24,540 --> 00:11:25,920
OK, try that one.

152
00:11:25,990 --> 00:11:28,940
So we don't have to deal with Fido and Whiskers again.

153
00:11:30,480 --> 00:11:31,730
Thirty three and thirty three.

154
00:11:31,950 --> 00:11:40,020
So what it did is it did the order of operations to solve for these values here and stored that in Y

155
00:11:40,470 --> 00:11:47,040
and then it also tied X as a shortcut to the same data that Y is tied to as a shortcut.

156
00:11:47,310 --> 00:11:54,930
So those variables have the same value because they use the assignment operation to say R, X equals

157
00:11:54,930 --> 00:12:01,260
Y, so Y equals X, OK, and then just as a review.

158
00:12:02,290 --> 00:12:03,610
We'll just show you that.

159
00:12:04,720 --> 00:12:11,290
If we change, X equals X times five.

160
00:12:14,150 --> 00:12:20,420
That's the same as doing X times equals five.

161
00:12:22,410 --> 00:12:28,110
All right, so we'll just show you what the console log is after each of those cases,

162
00:12:31,650 --> 00:12:34,700
we know it's thirty three, so let's take thirty three times five.

163
00:12:34,710 --> 00:12:37,020
That would be approximately one hundred and sixty five.

164
00:12:39,310 --> 00:12:42,190
So that's the scene right there.

165
00:12:43,860 --> 00:12:45,010
One hundred sixty five.

166
00:12:46,650 --> 00:12:50,310
And now let's just move it down here.

167
00:12:52,370 --> 00:12:58,970
And let's hide these lines, OK, so it's only going to use this and multiply that by five and we should

168
00:12:58,970 --> 00:13:01,070
get one sixty five still.

169
00:13:02,660 --> 00:13:07,580
We still get one sixty five and how do we know that we're not just seeing our old code?

170
00:13:08,360 --> 00:13:09,220
We'll do one time.

171
00:13:09,230 --> 00:13:11,180
We'll do that time six.

172
00:13:11,210 --> 00:13:11,720
There you go.

173
00:13:11,720 --> 00:13:13,250
See it added another thirty three.

174
00:13:15,130 --> 00:13:24,520
OK, so those are the same X equals X times five and X times equals five, so let's go ahead and wrap

175
00:13:24,520 --> 00:13:24,760
it up.

176
00:13:24,760 --> 00:13:26,950
But that is operator precedence.

177
00:13:26,950 --> 00:13:32,980
So showing you the order of operations for JavaScript that you're going to have to solve parentheses

178
00:13:32,980 --> 00:13:33,580
first.

179
00:13:34,000 --> 00:13:40,000
A few other things first, then you'll do the arithmetic operations and then you'll do.

180
00:13:40,990 --> 00:13:45,040
The logical operations and then so on from there.

181
00:13:46,230 --> 00:13:48,000
OK, see you in the next video.
