1
00:00:02,190 --> 00:00:06,210
With numbers, strings, arrays, and objects,

2
00:00:06,210 --> 00:00:11,130
we now got four super important types of values

3
00:00:11,130 --> 00:00:14,453
that you will use a lot in JavaScript programming.

4
00:00:15,310 --> 00:00:17,260
Now let's build up on that knowledge.

5
00:00:17,260 --> 00:00:20,040
And one thing we haven't done too much,

6
00:00:20,040 --> 00:00:23,430
or actually not at all up to this point,

7
00:00:23,430 --> 00:00:27,150
is work with those values.

8
00:00:27,150 --> 00:00:31,400
Sure, we use the alert command to output them,

9
00:00:31,400 --> 00:00:33,850
but very often you want to do

10
00:00:33,850 --> 00:00:36,540
calculations, transformations.

11
00:00:36,540 --> 00:00:38,810
You want to do operations like that

12
00:00:38,810 --> 00:00:40,820
with your data.

13
00:00:40,820 --> 00:00:42,550
And therefore, I now want to explore

14
00:00:42,550 --> 00:00:46,743
some important basics related to that with you now.

15
00:00:47,820 --> 00:00:51,700
For this I'll delete these two alert lines here,

16
00:00:51,700 --> 00:00:53,890
and now let's say we want to derive

17
00:00:53,890 --> 00:00:57,710
for how many years this user,

18
00:00:57,710 --> 00:01:01,420
which we are describing here, is an adult,

19
00:01:01,420 --> 00:01:03,710
assuming that you become an adult

20
00:01:03,710 --> 00:01:06,743
once you reach 18 years.

21
00:01:07,710 --> 00:01:11,900
Now for that, we could add a new variable

22
00:01:11,900 --> 00:01:15,630
that stores our result of that calculation,

23
00:01:15,630 --> 00:01:18,140
which we could name adultYears.

24
00:01:19,780 --> 00:01:21,160
Again, the name is up to you

25
00:01:21,160 --> 00:01:25,150
as long as you follow the rules and the convention,

26
00:01:25,150 --> 00:01:29,920
but the name of course also should convey some meaning

27
00:01:29,920 --> 00:01:33,360
and describe the value which you will store

28
00:01:33,360 --> 00:01:35,140
in this variable.

29
00:01:35,140 --> 00:01:38,580
And here I plan on storing the number of years

30
00:01:38,580 --> 00:01:41,583
for which this person here already is an adult.

31
00:01:42,700 --> 00:01:45,390
Now, the interesting thing here now

32
00:01:45,390 --> 00:01:50,090
is that now I don't want to hard code a new value.

33
00:01:50,090 --> 00:01:53,800
Of course, I could store 14 in there

34
00:01:53,800 --> 00:01:56,260
because I quickly calculated in my head

35
00:01:56,260 --> 00:02:00,360
that 32 minus 18 is 14,

36
00:02:00,360 --> 00:02:02,290
but that's not the idea here.

37
00:02:02,290 --> 00:02:04,700
This input here could be coming from a user

38
00:02:04,700 --> 00:02:05,930
through a form.

39
00:02:05,930 --> 00:02:09,550
So even though we did hard coded here

40
00:02:09,550 --> 00:02:12,260
as a developer for demo purposes,

41
00:02:12,260 --> 00:02:14,710
this value 32

42
00:02:14,710 --> 00:02:17,280
might actually not really be known by us,

43
00:02:17,280 --> 00:02:18,830
the developer.

44
00:02:18,830 --> 00:02:20,670
And therefore, we want to derive

45
00:02:20,670 --> 00:02:22,640
the adult years dynamically.

46
00:02:22,640 --> 00:02:24,750
We want to calculate them.

47
00:02:24,750 --> 00:02:27,020
Also of course, to avoid the fact

48
00:02:27,020 --> 00:02:29,860
that we make an error

49
00:02:29,860 --> 00:02:33,290
whilst doing that math in our head.

50
00:02:33,290 --> 00:02:36,700
So therefore, instead of just the writing 14 here,

51
00:02:36,700 --> 00:02:39,370
I instead want to use my age,

52
00:02:39,370 --> 00:02:41,560
which stores the 32,

53
00:02:41,560 --> 00:02:46,090
and now deduct 18 from that age.

54
00:02:46,090 --> 00:02:50,490
And you can do all those basic math operations,

55
00:02:50,490 --> 00:02:53,560
which you know, in JavaScript as well.

56
00:02:53,560 --> 00:02:57,330
You can deduct something by adding the minus symbol

57
00:02:57,330 --> 00:02:59,700
and then the value you want to deduct,

58
00:02:59,700 --> 00:03:01,293
18 in this case.

59
00:03:02,510 --> 00:03:04,870
And now adultYears will store

60
00:03:04,870 --> 00:03:07,170
the result of this operation.

61
00:03:07,170 --> 00:03:10,110
And of course, this could also be a longer operation

62
00:03:10,110 --> 00:03:12,170
with multiple steps.

63
00:03:12,170 --> 00:03:14,040
So you're not just allowed

64
00:03:14,040 --> 00:03:16,300
to deduct one value from another,

65
00:03:16,300 --> 00:03:19,483
but you can add as many steps here as you want.

66
00:03:21,250 --> 00:03:24,740
Of course, you're also not limited to subtraction.

67
00:03:24,740 --> 00:03:28,820
You can also add values with the plus symbol,

68
00:03:28,820 --> 00:03:33,160
multiply values with the asterisk symbol,

69
00:03:33,160 --> 00:03:37,283
and divide by values with the forward slash.

70
00:03:38,580 --> 00:03:40,800
But here we need the subtraction

71
00:03:40,800 --> 00:03:44,200
and now adultYears should store the result of age,

72
00:03:44,200 --> 00:03:48,173
which in this case is 32, minus 18.

73
00:03:49,590 --> 00:03:52,020
And I will, again, alert this here.

74
00:03:52,020 --> 00:03:54,550
I will alert adultYears,

75
00:03:54,550 --> 00:03:59,100
save that, and on my website,

76
00:03:59,100 --> 00:04:03,713
I therefore get 14 here in this alert box.

77
00:04:04,610 --> 00:04:07,680
So that's working, and that's important.

78
00:04:07,680 --> 00:04:11,100
This might sound obvious, and if it is, great,

79
00:04:11,100 --> 00:04:15,400
but this is a key thing in programming, in general,

80
00:04:15,400 --> 00:04:18,360
that you can, of course, not just use variables

81
00:04:18,360 --> 00:04:22,320
to store values so that you can then output them,

82
00:04:22,320 --> 00:04:25,510
but that you can also work with values

83
00:04:25,510 --> 00:04:28,680
and do mathematical or other operations

84
00:04:28,680 --> 00:04:31,670
on them to derive new values

85
00:04:31,670 --> 00:04:33,920
with which you then might do something,

86
00:04:33,920 --> 00:04:35,210
like in this case

87
00:04:35,210 --> 00:04:37,453
outputting that new value again.

88
00:04:38,980 --> 00:04:41,450
I also want to mention again

89
00:04:41,450 --> 00:04:44,390
that variables are called variables

90
00:04:44,390 --> 00:04:46,740
because the values stored in them

91
00:04:46,740 --> 00:04:49,260
are allowed to change.

92
00:04:49,260 --> 00:04:51,830
And at the moment that's never the case.

93
00:04:51,830 --> 00:04:53,990
We always assign a value,

94
00:04:53,990 --> 00:04:55,930
and then we leave it like that.

95
00:04:55,930 --> 00:04:58,580
You can also reassign variables.

96
00:04:58,580 --> 00:05:01,980
You're not limited to that initial value.

97
00:05:01,980 --> 00:05:04,630
So for example, if the age would change

98
00:05:04,630 --> 00:05:06,440
for some reason,

99
00:05:06,440 --> 00:05:08,930
you can again reference age

100
00:05:08,930 --> 00:05:10,320
and assign a new value

101
00:05:10,320 --> 00:05:12,510
by adding the equal sign again.

102
00:05:12,510 --> 00:05:15,913
And then you could store a value of, let's say, 45 here.

103
00:05:17,120 --> 00:05:19,690
Now the important thing here for that is

104
00:05:19,690 --> 00:05:23,450
that you don't add the let keyword again, though.

105
00:05:23,450 --> 00:05:25,390
You don't do that.

106
00:05:25,390 --> 00:05:27,750
You only add the let keyword

107
00:05:27,750 --> 00:05:32,180
when you first create and introduce a variable.

108
00:05:32,180 --> 00:05:34,480
Here, I did already create

109
00:05:34,480 --> 00:05:36,630
and introduce that variable

110
00:05:36,630 --> 00:05:39,310
and I'm then just assigning a new value,

111
00:05:39,310 --> 00:05:42,260
and that is then done without let.

112
00:05:42,260 --> 00:05:44,560
You only need let if you introduce

113
00:05:44,560 --> 00:05:47,263
a new variable with a new name.

114
00:05:48,440 --> 00:05:50,950
So here, for example, we need let.

115
00:05:50,950 --> 00:05:52,330
We are using age again,

116
00:05:52,330 --> 00:05:54,490
but only on the right side of the equal sign

117
00:05:54,490 --> 00:05:55,990
for this calculation.

118
00:05:55,990 --> 00:05:58,490
The variable, which we create, adultYears,

119
00:05:58,490 --> 00:06:00,130
is brand new.

120
00:06:00,130 --> 00:06:01,680
Here for age, on the other hand,

121
00:06:01,680 --> 00:06:03,460
we are using an existing variable,

122
00:06:03,460 --> 00:06:04,903
so we don't need let.

123
00:06:06,020 --> 00:06:09,510
And now that I did change age here,

124
00:06:09,510 --> 00:06:13,850
what do you think is the result of adultYears now?

125
00:06:13,850 --> 00:06:18,170
Is it still 14 because it still uses 32

126
00:06:18,170 --> 00:06:21,000
or will it be 27,

127
00:06:21,000 --> 00:06:24,133
the result of 45 minus 18?

128
00:06:25,630 --> 00:06:26,640
Well, let's see.

129
00:06:26,640 --> 00:06:31,040
If I save that and it reloads, I see 27.

130
00:06:31,040 --> 00:06:34,970
So it's the result of 45 minus 18.

131
00:06:34,970 --> 00:06:37,440
And that should make a lot of sense

132
00:06:37,440 --> 00:06:40,700
because instructions are executed step by step,

133
00:06:40,700 --> 00:06:45,130
line by line, and therefore we did change age

134
00:06:45,130 --> 00:06:47,190
before we use it in this calculation,

135
00:06:47,190 --> 00:06:50,070
and hence the new value of age is used,

136
00:06:50,070 --> 00:06:51,960
not the old value.

137
00:06:51,960 --> 00:06:54,830
That wouldn't make too much sense.

138
00:06:54,830 --> 00:06:57,060
So therefore, in this demo script,

139
00:06:57,060 --> 00:07:00,760
we now never use the original age of 32.

140
00:07:00,760 --> 00:07:02,910
We only use the new one.

141
00:07:02,910 --> 00:07:05,110
And of course, we're still just playing around

142
00:07:05,110 --> 00:07:06,390
with these features here

143
00:07:06,390 --> 00:07:08,780
to get a feeling for what we can do

144
00:07:08,780 --> 00:07:10,393
with JavaScript.

