1
00:00:00,510 --> 00:00:02,440
OK, welcome back to lesson three.

2
00:00:02,970 --> 00:00:04,560
We're going to talk about variables.

3
00:00:05,250 --> 00:00:06,840
So what is a variable?

4
00:00:07,170 --> 00:00:13,170
If you're taking math classes and especially algebra and calculus, you've probably heard of variables,

5
00:00:13,170 --> 00:00:21,330
basically what they are ideas for, just basically a shortcut that will refer to some other piece of

6
00:00:21,330 --> 00:00:23,430
information in math.

7
00:00:23,430 --> 00:00:26,690
It's usually numbers, but in programming it could be anything.

8
00:00:26,690 --> 00:00:29,160
It could be some text, it could be a number.

9
00:00:29,160 --> 00:00:30,840
It could be a true or false value.

10
00:00:31,380 --> 00:00:37,170
It could be a set of other pieces of data, which are some of those other values as well.

11
00:00:38,260 --> 00:00:46,840
OK, so in this context, a variable is going to be basically picture a box and you put stuff into that

12
00:00:46,840 --> 00:00:51,100
box and then what, you label that box, that's what the variable is.

13
00:00:52,210 --> 00:00:58,270
So another way to look at it is probably closer to what's accurate for JavaScript is that the variable

14
00:00:58,270 --> 00:00:59,030
is a shortcut.

15
00:00:59,980 --> 00:01:05,460
So if you think about shortcuts on your computer, that is a file that links to another file.

16
00:01:06,310 --> 00:01:08,710
And so that's a lot like what variables are.

17
00:01:09,010 --> 00:01:14,110
You're really just assigning some data to a specific variable name.

18
00:01:14,770 --> 00:01:20,710
And then that variable name is used as the shortcut to arrive at the data that you assigned to it.

19
00:01:21,940 --> 00:01:27,520
In JavaScript, we have three different types of variables, we have VAR, which is the one that's been

20
00:01:27,520 --> 00:01:29,080
around since nineteen ninety five.

21
00:01:30,090 --> 00:01:37,560
And we have let which has been around since at least twenty fifteen and Consed as well since twenty

22
00:01:37,560 --> 00:01:39,870
fifteen with Essex.

23
00:01:40,950 --> 00:01:47,670
Now the reason we added Let and Consed to JavaScript is that they are a lot more strict and specific

24
00:01:48,090 --> 00:01:52,890
and they are block scoped as opposed to VARs which are function scoped.

25
00:01:54,800 --> 00:02:02,990
Because let and our block scoped and they're more specific, they are much easier to use in a development

26
00:02:02,990 --> 00:02:09,570
environment that is calling for strict typing and trying to prevent some common errors from happening.

27
00:02:09,770 --> 00:02:12,560
So we're just going to use Lett and Consed for the rest of our time.

28
00:02:13,220 --> 00:02:19,820
And a rule of thumb is that you should use CONSED until you can't and then you should use let the reason

29
00:02:19,820 --> 00:02:24,180
const and let are different is that const is a constant variable.

30
00:02:24,200 --> 00:02:30,830
It means once you set it for the first time and give it its first assigned value, it can no longer

31
00:02:30,830 --> 00:02:33,590
be edited with a different value for the same data type.

32
00:02:34,870 --> 00:02:40,570
Let is a little bit different, it can be reassigned, so that means you can mutate it and give it a

33
00:02:40,570 --> 00:02:42,340
different value.

34
00:02:42,700 --> 00:02:48,730
However many times you want to make some basic rules that apply to all of these is that you can't give

35
00:02:48,730 --> 00:02:50,770
a variable name that starts with a number.

36
00:02:51,490 --> 00:02:59,290
So you can't have a variable called three or something like that or even have a variable that three

37
00:02:59,290 --> 00:03:00,380
test or something.

38
00:03:01,480 --> 00:03:05,560
It also can't contain symbols except for the dollar sign and the underscore.

39
00:03:06,520 --> 00:03:11,500
And it can't be a reserved keyword, like the word function or the word F or the word delete.

40
00:03:11,930 --> 00:03:17,950
These are special keywords in JavaScript that need to be used and understood only in the context of

41
00:03:17,950 --> 00:03:19,210
their special powers.

42
00:03:20,440 --> 00:03:25,390
And the best practices for naming variables is simply use a semantic naming convention.

43
00:03:25,720 --> 00:03:28,330
Semantics is just the study of meaning.

44
00:03:29,200 --> 00:03:33,730
And so a semantic naming convention means have a meaningful name for your variable.

45
00:03:34,180 --> 00:03:39,700
If you're variable is going to store a bunch of numbers, then maybe you should call it no list.

46
00:03:40,510 --> 00:03:46,480
Or if you're variable is storing a person's name, maybe your variables should also be the name of that

47
00:03:46,480 --> 00:03:50,320
person, or it should be person one or something like that.

48
00:03:51,880 --> 00:03:57,880
Camel casing is a standard for variable names in JavaScript and then there are a few other standards

49
00:03:58,360 --> 00:04:00,010
depending on the type of variable.

50
00:04:00,130 --> 00:04:06,940
One common suggestion is that if you have a cost variable, it is a constant variable that's used as

51
00:04:06,940 --> 00:04:11,150
a constant for your whole script that you do all caps for that variable.

52
00:04:11,320 --> 00:04:13,510
These rules are not ironclad.

53
00:04:13,870 --> 00:04:19,570
As long as you are consistent across your own scripts, then it's something that people can understand

54
00:04:19,570 --> 00:04:19,930
well.

55
00:04:20,110 --> 00:04:26,710
So to get started, if you don't have a preference, go ahead and use Camil case for your regular variables

56
00:04:27,070 --> 00:04:29,950
and we'll just use Kemakeza Keys for all variables in our examples.

57
00:04:30,440 --> 00:04:33,640
OK, so let's take a look at some of these variables in action.

58
00:04:34,260 --> 00:04:38,050
OK, so as you can see, we're starting with the example that we ended with last time.

59
00:04:38,650 --> 00:04:46,750
We have Animal is hungry and age shows you the examples, what it looks like when we consider this information.

60
00:04:53,010 --> 00:05:00,510
We're going to talk a word wrap so we can see this easier, and then now let's look at another example.

61
00:05:01,750 --> 00:05:02,150
OK.

62
00:05:06,230 --> 00:05:11,690
I'm going to show you a few other things that we can use to manipulate our window as well.

63
00:05:12,740 --> 00:05:17,840
OK, so let's add to this and add our first and last name.

64
00:05:23,630 --> 00:05:30,680
And for the last name, we could make it specific or we could also do a prompt.

65
00:05:32,600 --> 00:05:36,830
Prompt is going to tell our browser to ask us for what it's going to be.

66
00:05:43,510 --> 00:05:48,640
And then we'll be able to store that value in that new variable, so then when we want to consider,

67
00:05:50,770 --> 00:05:53,200
we'll be able to just say first name.

68
00:06:01,060 --> 00:06:01,720
Last name.

69
00:06:02,550 --> 00:06:08,020
OK, so then it's going to print out here the first name and last name.

70
00:06:09,010 --> 00:06:10,690
Let's save that from this again.

71
00:06:12,210 --> 00:06:13,240
What is his last name?

72
00:06:14,020 --> 00:06:14,530
Elsworth.

73
00:06:14,620 --> 00:06:15,520
That's my last name.

74
00:06:16,730 --> 00:06:24,470
Paice, Elsworth, cool, so we told it with the first name was, and then we asked the script to for

75
00:06:24,470 --> 00:06:25,640
a different last name.

76
00:06:26,450 --> 00:06:27,410
Let's try that again.

77
00:06:28,500 --> 00:06:29,900
Let's just do Case Jones.

78
00:06:31,130 --> 00:06:34,310
As you can see, it's going to take whatever we tell it.

79
00:06:35,330 --> 00:06:40,370
So that's the power of programming is some dynamic interactive information.

80
00:06:42,940 --> 00:06:49,960
So this is what variable mutation is, is that we're actually changing the value of this variable depending

81
00:06:49,960 --> 00:06:57,370
on something else, so we can change it manually in the code, we could have it change based on a user

82
00:06:57,370 --> 00:06:59,740
input, and that's called mutation.

83
00:07:00,970 --> 00:07:03,940
And so that was similar to an alert.

84
00:07:04,130 --> 00:07:05,890
We could also alert the same thing.

85
00:07:09,890 --> 00:07:17,500
We just do an alert with the same information, unlike a console, that it's not going to output this

86
00:07:17,500 --> 00:07:19,060
information only to the console.

87
00:07:19,060 --> 00:07:21,730
It's going to put it in a pop-up.

88
00:07:24,540 --> 00:07:25,800
Let's do Smith this time.

89
00:07:27,580 --> 00:07:33,520
So Smith going in the console because of line twenty two, but Psmith showing up here in this alert

90
00:07:34,120 --> 00:07:35,270
because of this alert line.

91
00:07:36,070 --> 00:07:43,270
OK, now you know that prompts do a pop up and ask for information and alerts, do pop ups that just

92
00:07:43,270 --> 00:07:44,320
output information.

93
00:07:46,400 --> 00:07:49,360
How to be more specific about the differences between the three.

94
00:07:49,630 --> 00:07:53,350
Let's take a look at VA and let.

95
00:07:55,710 --> 00:07:56,430
And Consed

96
00:07:59,700 --> 00:08:00,420
and Consed.

97
00:08:09,370 --> 00:08:10,660
So if I do a var.

98
00:08:12,970 --> 00:08:19,540
Test bar, then I can set this to any piece of data that I want.

99
00:08:21,870 --> 00:08:23,190
And then I can reassign it.

100
00:08:27,670 --> 00:08:28,050
OK.

101
00:08:29,450 --> 00:08:40,490
I can also specify this without using var, so because var is the default, if I just do test bar,

102
00:08:40,880 --> 00:08:43,430
then it will understand it as a var type.

103
00:08:43,950 --> 00:08:45,740
It won't understand it as a type.

104
00:08:46,650 --> 00:08:50,560
OK, so this can be declared, it can also be redesigned.

105
00:08:50,690 --> 00:08:59,960
So this is a declaration right here because I stated the variable type and the variable name and it's

106
00:08:59,960 --> 00:09:07,610
an assignment OK, because I also assigned it this variable right here with this assignment operator

107
00:09:07,610 --> 00:09:09,320
equals sign and the number.

108
00:09:11,100 --> 00:09:17,300
This is just an assignment, so this is a reassignment also known as a mutation.

109
00:09:19,620 --> 00:09:19,970
OK.

110
00:09:22,600 --> 00:09:29,380
And this also is a declaration and assignment, but because it's a VA, it doesn't require me to state

111
00:09:30,040 --> 00:09:39,340
the word VA, but unfortunately I can also read declare the same variable.

112
00:09:42,260 --> 00:09:44,810
And this isn't necessarily a problem.

113
00:09:46,220 --> 00:09:56,330
For JavaScript, so it's a re declaration and assignment, but the problem with this is that it's not

114
00:09:56,330 --> 00:09:57,230
very clean code.

115
00:09:57,950 --> 00:10:03,750
Unfortunately, what this means is you don't have very clean code when you reuse the same variable re

116
00:10:03,840 --> 00:10:04,520
declaring it.

117
00:10:05,090 --> 00:10:09,380
What you're saying is that you don't have a very semantic purpose for this variable.

118
00:10:09,590 --> 00:10:11,930
It's not being used in a very specific way.

119
00:10:12,740 --> 00:10:17,990
So instead we prefer to use let because it does prevent those types of errors from happening.

120
00:10:18,530 --> 00:10:20,260
Let's go ahead and comment this out.

121
00:10:20,390 --> 00:10:21,620
Let's clean this up a little bit.

122
00:10:23,660 --> 00:10:34,700
So with let if we were to do a test for sea, equal for that would be totally fine.

123
00:10:34,850 --> 00:10:36,920
There's a declaration and assignment there as well.

124
00:10:38,120 --> 00:10:40,070
What if I tried to do it again?

125
00:10:43,010 --> 00:10:44,230
We would run into an error.

126
00:10:49,100 --> 00:10:53,030
And there would be a syntax error that test Farsi has already been declared.

127
00:10:53,950 --> 00:11:01,390
So this is really good, let prevents us from reusing our variable names in ways that would cause our

128
00:11:01,390 --> 00:11:02,710
code to be more confusing.

129
00:11:04,570 --> 00:11:10,930
But we are able to very simply take this variable, however we like is totally fine.

130
00:11:13,190 --> 00:11:19,580
Consed is the one that prevents us from modifying this variable.

131
00:11:20,330 --> 00:11:24,400
It can be declared with a number, whatever variable we want to give to it.

132
00:11:24,800 --> 00:11:30,320
But notice that the color is a little bit different on here, and that's by design in visual studio

133
00:11:30,320 --> 00:11:30,650
code.

134
00:11:31,860 --> 00:11:38,120
I can't modify this now with another number if I try.

135
00:11:39,510 --> 00:11:44,880
And run it, it will say uncaught type error assignment to constant variable.

136
00:11:45,830 --> 00:11:51,050
As a result of trying to assign this to a constant variable, it shouts at me and says, no, this is

137
00:11:51,050 --> 00:11:52,460
a constant, constant means.

138
00:11:52,460 --> 00:11:53,570
It does not change.

139
00:11:54,290 --> 00:11:57,890
So therefore, I cannot reassign or redeclared.

140
00:11:59,330 --> 00:12:02,330
You can reassign, but you cannot declare.

141
00:12:06,820 --> 00:12:15,840
OK, let OK, so that is it for just to go into a little bit more depth here, if we were to set a new

142
00:12:15,840 --> 00:12:16,500
variable up.

143
00:12:24,610 --> 00:12:29,140
But we did not give it any value and then we canceled it.

144
00:12:37,460 --> 00:12:38,830
And then we see what happens.

145
00:12:43,570 --> 00:12:46,390
There is a syntax error at line forty five.

146
00:12:47,360 --> 00:12:50,630
That we're missing in initialism in the declaration.

147
00:12:53,060 --> 00:12:53,960
So that's not good.

148
00:12:55,580 --> 00:13:01,250
What if we turn this into Aulet and it doesn't require an initial value, but it is undefined?

149
00:13:02,330 --> 00:13:03,470
So it's going to start out.

150
00:13:03,950 --> 00:13:07,370
Let's go ahead and clean this up, so it's going to start out undefined.

151
00:13:08,860 --> 00:13:09,820
So as a result.

152
00:13:11,340 --> 00:13:18,390
It doesn't have any information to start out with now, if we were to do know, it would have a value

153
00:13:18,390 --> 00:13:21,770
of no, but that value would essentially be empty.

154
00:13:22,050 --> 00:13:25,520
So we can change this to be null.

155
00:13:27,060 --> 00:13:28,650
And that's a special keyword as well.

156
00:13:29,850 --> 00:13:30,750
And if we do that.

157
00:13:33,040 --> 00:13:34,650
Let's put that right here.

158
00:13:36,460 --> 00:13:43,240
And save it, so now, instead of being undefined, it is null, it still means it's empty, but we're

159
00:13:43,240 --> 00:13:44,840
explicitly telling it that it's empty.

160
00:13:44,890 --> 00:13:48,220
So at this point, this is undefined at this point.

161
00:13:48,370 --> 00:13:50,450
This is no, that is it for now.

162
00:13:50,470 --> 00:13:51,820
So we learned about variables.

163
00:13:51,970 --> 00:13:58,830
We learned about the difference between VAR Let and Consed and we learned that there are certain limits.

164
00:13:59,080 --> 00:14:01,150
You can't start variables with numbers.

165
00:14:01,750 --> 00:14:06,370
You can't have variable names that symbols other than the dollar sign of the underscore.

166
00:14:06,910 --> 00:14:11,920
And they can't be special reserved keywords like function if or delete and best practice definitely

167
00:14:11,920 --> 00:14:14,430
use a variable name that is meaningful.

168
00:14:14,440 --> 00:14:20,470
So it's easy to read your code and we prefer to use Camil case and some other ways of.

169
00:14:21,670 --> 00:14:24,700
Reading very clearly, as opposed to having everything lowercase.

170
00:14:24,760 --> 00:14:27,570
OK, well, we'll come back and we'll see you next lesson.

171
00:14:27,580 --> 00:14:29,200
We're going to talk about hoisting.
