1
00:00:00,560 --> 00:00:05,600
In this video we're going to focus on Javascript variables and their syntax.

2
00:00:05,940 --> 00:00:10,290
So variables are a concept that exists in every programming language out there.

3
00:00:10,290 --> 00:00:12,810
And the basic concept is always the same.

4
00:00:12,900 --> 00:00:19,530
A variable is a container that has a name on it and inside of that container we store some bit of data

5
00:00:19,550 --> 00:00:20,070
.

6
00:00:20,610 --> 00:00:23,010
So we saw the basic primitive types.

7
00:00:23,010 --> 00:00:28,920
Things like strings and numbers and booleans and variables are just a place a container a jar where

8
00:00:28,920 --> 00:00:32,410
we can put one of those in and give it a name that we refer to later.

9
00:00:32,580 --> 00:00:38,460
And then of course the name variable means that the data that we store in that jar in the container

10
00:00:38,550 --> 00:00:41,280
can change you can vary.

11
00:00:41,280 --> 00:00:42,970
So variables are really important.

12
00:00:43,020 --> 00:00:45,810
They let us store some data and recall it later.

13
00:00:45,900 --> 00:00:53,160
So we could have a variable called Current logged in user if we had a website with user log in and it

14
00:00:53,160 --> 00:00:56,780
would just know the user name of the person who was logged in.

15
00:00:57,030 --> 00:00:58,910
And it could vary and it would change.

16
00:00:58,950 --> 00:01:05,820
And then as soon as someone logs out it's then set to be empty or false or something we could have another

17
00:01:05,820 --> 00:01:13,230
variable for our tic tac toe game called Current turn and it would switch between player 1 and player

18
00:01:13,230 --> 00:01:20,010
to player 1 and Player 2 and that variable is in charge of knowing or keeping track of which player

19
00:01:20,010 --> 00:01:22,800
is supposed to be going at any given point.

20
00:01:22,890 --> 00:01:26,520
So the syntax for variables in javascript is very simple.

21
00:01:26,520 --> 00:01:28,290
It looks like this.

22
00:01:28,290 --> 00:01:33,600
The VAR keyword V-A are space a variable name.

23
00:01:33,600 --> 00:01:40,170
This is up to us whatever we want the name to be called the label for that container equal sign and

24
00:01:40,170 --> 00:01:44,460
then a value and then a semi-colon.

25
00:01:44,460 --> 00:01:47,600
We will talk a lot more about when you need semi colons.

26
00:01:47,700 --> 00:01:53,370
For now everything we do will be able to get away with a semi-colon or not having some a colon.

27
00:01:53,640 --> 00:01:57,260
When we talk about style and convention we'll talk about that.

28
00:01:57,390 --> 00:01:59,960
But for now put a semi-colon at the end.

29
00:02:00,870 --> 00:02:03,810
So here's three examples of variables.

30
00:02:03,900 --> 00:02:06,410
The first one far name.

31
00:02:06,480 --> 00:02:14,120
So I'm calling this container name and I'm setting it equal to the string Rusty the next one.

32
00:02:14,310 --> 00:02:20,430
I'm creating a variable called secret number and I'm setting it equal to the number 73 and the last

33
00:02:20,430 --> 00:02:26,260
one and making a variable is adorable and setting it equal to be true the boolean value.

34
00:02:26,640 --> 00:02:28,100
So let me show you how this works.

35
00:02:28,110 --> 00:02:30,030
I'm going to go ahead and open up my Consul here

36
00:02:32,730 --> 00:02:36,480
and I'm going to go ahead and make a first variable.

37
00:02:36,480 --> 00:02:45,810
So my variable will be called name and I'm going to set an equal to my wonderful dog Rusty's name and

38
00:02:45,810 --> 00:02:53,070
I'll hit enter and what I've done now is say javascript I need you to make some space and I need you

39
00:02:53,070 --> 00:02:56,420
to give that space a name called name.

40
00:02:56,430 --> 00:02:59,110
It's a little confusing instead of equal to resti.

41
00:02:59,130 --> 00:03:03,420
So now whenever I ask for a name I just type the word name.

42
00:03:03,420 --> 00:03:08,480
It's going to give me Rustie or whatever is stored in name.

43
00:03:08,580 --> 00:03:11,890
So the next thing is that I can change the value that's in there.

44
00:03:12,240 --> 00:03:14,950
So to do that I just reassign it.

45
00:03:15,060 --> 00:03:24,610
I just say name equal sign and I'll change it to be Tator that is terrible nickname I give him.

46
00:03:24,620 --> 00:03:29,780
And now if I ask for name again it's now Tator.

47
00:03:29,820 --> 00:03:31,610
So that's the important part of variables.

48
00:03:31,620 --> 00:03:32,700
They can change.

49
00:03:32,700 --> 00:03:34,140
They are variable.

50
00:03:34,710 --> 00:03:40,750
So in this example I'm recalling the value of name and then concatenating it with Hello there.

51
00:03:40,860 --> 00:03:42,430
So I'll just show you that.

52
00:03:42,570 --> 00:03:50,090
Let's make another variable this time called friend with our friends equals Sally.

53
00:03:50,820 --> 00:03:54,930
And then all I do is say friend let's do a hello there.

54
00:03:55,350 --> 00:03:57,180
Hello there.

55
00:03:57,720 --> 00:04:03,750
Plus friend and I made sure to add a space here so that when we add them together we have a nice space

56
00:04:03,750 --> 00:04:07,520
in our sentence and we get hello there Sally.

57
00:04:08,100 --> 00:04:15,330
And then if I change that and I now say friend is equal to no one.

58
00:04:15,440 --> 00:04:17,180
It's very sad.

59
00:04:18,360 --> 00:04:24,740
And I write the same line Hello there plus friend I now get hello there no one.

60
00:04:25,860 --> 00:04:29,260
So also just show to you we can store numbers in there as well.

61
00:04:29,350 --> 00:04:31,210
Can or any of the basic data types.

62
00:04:31,410 --> 00:04:37,110
So I'll make a number called Far numb equals.

63
00:04:37,320 --> 00:04:41,610
Let's go with forty three.

64
00:04:41,610 --> 00:04:47,870
And now whenever I refer to Nom I get 43 and I can do math with that so I can do something like no.

65
00:04:48,100 --> 00:05:00,540
Plus one hundred and I get 143 or I can do numb my mide two and I get one.

66
00:05:00,540 --> 00:05:05,040
Another important concept about variables and Javascript is that I can change their value but I can

67
00:05:05,040 --> 00:05:06,990
also change their type.

68
00:05:06,990 --> 00:05:14,910
So let's say that I want to change the value of knowm to the string 50.

69
00:05:15,360 --> 00:05:18,470
I can do that very easily like this numb equals.

70
00:05:19,050 --> 00:05:27,450
And I could either write that number 50 in there or the string the word version of 50 and it's OK that

71
00:05:27,480 --> 00:05:28,410
it's a string.

72
00:05:28,410 --> 00:05:31,170
It used to be a number before hand but I now change it to a string.

73
00:05:31,170 --> 00:05:35,200
Javascript doesn't care as a side note in some languages.

74
00:05:35,280 --> 00:05:37,290
That is a problem and you can't do that.

75
00:05:37,290 --> 00:05:42,660
Javascript has something called dynamic typing which means that it can change from one type to another

76
00:05:42,750 --> 00:05:43,870
without problem.

77
00:05:44,220 --> 00:05:48,630
So the last thing about variables that I want to touch on is that there's a convention for how we name

78
00:05:48,630 --> 00:05:49,230
them.

79
00:05:49,770 --> 00:05:56,540
And if you notice here they all follow camel case and that means that the first letter is always lowercase

80
00:05:56,540 --> 00:05:56,820
.

81
00:05:57,090 --> 00:06:02,490
And then if we have another word we uppercase the first letter of the new word.

82
00:06:02,760 --> 00:06:12,450
So camel case looks like this camel case and snake case which is another case you may hear uses underscores

83
00:06:12,810 --> 00:06:21,930
between words and there's one more often called Khabab case or Dasch case which is you use a dash between

84
00:06:21,930 --> 00:06:22,590
words.

85
00:06:22,770 --> 00:06:25,140
So javascript variables should be camel cased
