1
00:00:02,520 --> 00:00:06,390
It's also worth pointing out, and I mentioned this before,

2
00:00:06,390 --> 00:00:08,250
but I want to emphasize it again.

3
00:00:08,250 --> 00:00:13,250
That of course you are not limited to two numbers only

4
00:00:14,030 --> 00:00:16,490
when you perform a certain operation.

5
00:00:16,490 --> 00:00:18,950
It is perfectly fine to have

6
00:00:18,950 --> 00:00:22,269
an operation like this, for example.

7
00:00:22,269 --> 00:00:27,269
You can use as many values as you want in such operation.

8
00:00:27,993 --> 00:00:31,630
You can, of course also use floating point numbers.

9
00:00:31,630 --> 00:00:35,200
So decimal numbers here as well, by using a dot

10
00:00:35,200 --> 00:00:38,480
as a separator between the decimal place

11
00:00:38,480 --> 00:00:41,414
and the number in front of the decimal place,

12
00:00:41,414 --> 00:00:44,750
that's all possible.

13
00:00:44,750 --> 00:00:49,440
If you do use multiple values in your calculation though,

14
00:00:49,440 --> 00:00:51,720
as we do it here, and this is the result,

15
00:00:51,720 --> 00:00:54,600
then it is important to keep in mind

16
00:00:54,600 --> 00:00:58,307
that regular mathematical rules apply,

17
00:00:58,307 --> 00:01:01,670
which means that multiplication and division

18
00:01:01,670 --> 00:01:06,450
have a higher priority than using plus or minus.

19
00:01:06,450 --> 00:01:07,830
And then for here,

20
00:01:07,830 --> 00:01:10,890
this would be the first part that's evaluated.

21
00:01:10,890 --> 00:01:13,350
So we get 50 as a result,

22
00:01:13,350 --> 00:01:17,530
and then 50 would be deducted from ten plus three

23
00:01:17,530 --> 00:01:20,823
which is why we get -37 as a result.

24
00:01:21,920 --> 00:01:26,193
This might or might not be obvious, now it hopefully is.

25
00:01:27,280 --> 00:01:29,900
If you ever want to change these rules,

26
00:01:29,900 --> 00:01:33,920
you can do it just like you do it in regular math as well.

27
00:01:33,920 --> 00:01:38,190
You can add brackets, parentheses around the operation

28
00:01:38,190 --> 00:01:40,760
that should be evaluated first,

29
00:01:40,760 --> 00:01:43,930
because that now in turn has a higher priority

30
00:01:43,930 --> 00:01:46,550
then the regular priority.

31
00:01:46,550 --> 00:01:49,750
So now this term would be evaluated first

32
00:01:49,750 --> 00:01:51,890
and then the result of this term

33
00:01:51,890 --> 00:01:53,810
would be multiplied with ten.

34
00:01:53,810 --> 00:01:57,163
And that's why we now get a totally different result here.

35
00:01:58,330 --> 00:02:00,230
So that's what you should keep in mind

36
00:02:00,230 --> 00:02:04,730
about mathematical operations in your JavaScript code.

37
00:02:04,730 --> 00:02:06,290
Now, as I mentioned before,

38
00:02:06,290 --> 00:02:10,259
you can also store results of operations in variables.

39
00:02:10,259 --> 00:02:12,715
And I briefly want to come back to that.

40
00:02:12,715 --> 00:02:16,110
We can add a result variable,

41
00:02:16,110 --> 00:02:19,288
and we could store the result of this calculation

42
00:02:19,288 --> 00:02:20,650
in this variable.

43
00:02:20,650 --> 00:02:23,440
Or of any other calculation.

44
00:02:23,440 --> 00:02:26,750
Of course you can then also reassign the value.

45
00:02:26,750 --> 00:02:30,510
You can now store a new value in that variable,

46
00:02:30,510 --> 00:02:33,600
and that can be any kind of value.

47
00:02:33,600 --> 00:02:37,803
For example, I could then store ten times four in result.

48
00:02:39,040 --> 00:02:40,240
Now I'm emphasizing this

49
00:02:40,240 --> 00:02:44,140
because there is one way of changing a variable

50
00:02:44,140 --> 00:02:49,140
that stores a value that can be shortened a little bit.

51
00:02:49,420 --> 00:02:52,540
And for that, let's say that for result,

52
00:02:52,540 --> 00:02:54,900
which has 40 after this line here,

53
00:02:54,900 --> 00:02:58,210
or which holds a value of 40 after this line.

54
00:02:58,210 --> 00:03:01,260
We now want to add one to result.

55
00:03:01,260 --> 00:03:03,050
Now we can write it like this.

56
00:03:03,050 --> 00:03:06,323
This is perfectly fine JavaScript code.

57
00:03:06,323 --> 00:03:09,660
You can use a variable on the right side

58
00:03:09,660 --> 00:03:11,701
of such a variable assignment,

59
00:03:11,701 --> 00:03:14,880
even if you're assigning the result here,

60
00:03:14,880 --> 00:03:16,690
to the existing variable.

61
00:03:16,690 --> 00:03:18,930
Even if the variable you're assigning

62
00:03:18,930 --> 00:03:22,580
to is being used on the right side as well.

63
00:03:22,580 --> 00:03:26,560
This works because JavaScript will always evaluate

64
00:03:26,560 --> 00:03:29,989
and execute the right side of the equal sign first

65
00:03:29,989 --> 00:03:32,730
with the old value of result.

66
00:03:32,730 --> 00:03:37,290
So the value that is locked in, in line 13 so to say.

67
00:03:37,290 --> 00:03:40,700
And only once this calculation has been performed,

68
00:03:40,700 --> 00:03:43,730
the result of that calculation will be stored

69
00:03:43,730 --> 00:03:45,720
in that variable.

70
00:03:45,720 --> 00:03:49,880
So only then the variable value will change.

71
00:03:49,880 --> 00:03:51,660
That's why we can use a variable

72
00:03:51,660 --> 00:03:54,240
on the right side of the equal sign.

73
00:03:54,240 --> 00:03:57,343
Even if we're assigning to the very same variable.

74
00:03:58,260 --> 00:04:01,910
Now there are a couple of operations which are very common

75
00:04:01,910 --> 00:04:04,940
and therefore can be shortened, though.

76
00:04:04,940 --> 00:04:09,331
For example, if you add one to an existing variable.

77
00:04:09,331 --> 00:04:11,740
Then you can write it like this,

78
00:04:11,740 --> 00:04:14,990
or you can write it like this.

79
00:04:14,990 --> 00:04:16,250
This might look strange,

80
00:04:16,250 --> 00:04:18,320
but that is the same as saying

81
00:04:18,320 --> 00:04:23,025
result equals result plus one, it's just a shorter form.

82
00:04:23,025 --> 00:04:27,140
We've got something similar for subtracting one.

83
00:04:27,140 --> 00:04:29,653
Here we have result minus minus.

84
00:04:31,380 --> 00:04:32,440
Now sometimes of course,

85
00:04:32,440 --> 00:04:35,130
you don't just want to add or subtract one.

86
00:04:35,130 --> 00:04:38,630
Sometimes you maybe want to add five, let's say.

87
00:04:38,630 --> 00:04:41,780
In that case, you can't use the plus plus notation,

88
00:04:41,780 --> 00:04:45,170
but we still got a shorter version of writing this.

89
00:04:45,170 --> 00:04:48,170
You can write plus equals five,

90
00:04:48,170 --> 00:04:52,010
and this is equal to writing it like this.

91
00:04:52,010 --> 00:04:54,060
So whenever you use a variable

92
00:04:54,060 --> 00:04:56,460
on the right side of an equal sign

93
00:04:56,460 --> 00:04:59,860
and you then store the result in the very same variable,

94
00:04:59,860 --> 00:05:01,980
you can use shortcuts like this

95
00:05:01,980 --> 00:05:05,082
for simple mathematical operations.

96
00:05:05,082 --> 00:05:10,082
That also exists for minus, if you want to subtract

97
00:05:11,270 --> 00:05:15,030
and also for division and multiplication.

98
00:05:15,030 --> 00:05:20,030
So we can also shorten these statements here, if we want to.

99
00:05:20,400 --> 00:05:21,933
This all works.

100
00:05:24,720 --> 00:05:28,800
If I now console lock the result here at the bottom

101
00:05:28,800 --> 00:05:31,320
after changing it a couple of times,

102
00:05:31,320 --> 00:05:34,820
you will see that in that developer tools console,

103
00:05:34,820 --> 00:05:36,880
you've got a bunch of valid results

104
00:05:36,880 --> 00:05:40,550
from these lines of code that we added here.

105
00:05:40,550 --> 00:05:43,710
And these are just some shortcuts which can be useful

106
00:05:43,710 --> 00:05:46,400
if you're doing some mathematical operations

107
00:05:46,400 --> 00:05:49,350
and you are changing an existing variable

108
00:05:49,350 --> 00:05:52,400
based on its previous value.

109
00:05:52,400 --> 00:05:55,030
Now I will delete all of that for the moment,

110
00:05:55,030 --> 00:05:58,211
but these are a couple of shorthand notations

111
00:05:58,211 --> 00:06:01,090
that are worth keeping in mind.

112
00:06:01,090 --> 00:06:04,083
You will also encounter them again later in the course.

