1 00:00:00,460 --> 00:00:06,850 Welcome back to Section three, lesson five, declaration and assignment today we're going to be talking 2 00:00:06,850 --> 00:00:12,070 about variable declaration and assignment and how we use variables in JavaScript. 3 00:00:13,120 --> 00:00:22,210 Variable declaration is simply using varlet or const these special function keywords that we use to 4 00:00:22,240 --> 00:00:27,070 denote that we're initializing a variable and giving that variable a name. 5 00:00:27,730 --> 00:00:33,250 So in this situation, we would use either const Aulet or VAR and I can talk about the differences of 6 00:00:33,250 --> 00:00:39,640 those and then you just give your variable a name your variable cannot start with. 7 00:00:39,640 --> 00:00:41,740 Remember, it cannot start with a number. 8 00:00:42,470 --> 00:00:47,920 There are a few other rules, but for the most part you want to give your variable a name that is descriptive 9 00:00:48,220 --> 00:00:50,020 for what you're trying to accomplish. 10 00:00:50,170 --> 00:00:56,080 If you want the value of this to be something related to a car, car would be a very good name for your 11 00:00:56,080 --> 00:00:56,650 variable. 12 00:00:57,400 --> 00:01:03,550 If you wanted to have some number that you're probably going to change using some other code, then 13 00:01:03,760 --> 00:01:07,420 Count is a great name for that variable and you can make these whatever you want. 14 00:01:09,170 --> 00:01:15,530 Variable assignment is when you use the assignment operator, which is the equals sign to give the variable 15 00:01:15,530 --> 00:01:16,240 a value. 16 00:01:16,910 --> 00:01:19,130 So you're going to start with some value. 17 00:01:20,100 --> 00:01:27,450 Use the assignment operator and then your variable name, and that way they'll be tied together, variable 18 00:01:27,450 --> 00:01:35,520 assignment can be described either as putting this data into a box with this label or alternately thinking 19 00:01:35,520 --> 00:01:37,500 of the variable as a shortcut. 20 00:01:37,800 --> 00:01:40,710 Like on a computer, you can set a shortcut to open a file. 21 00:01:41,700 --> 00:01:45,630 The variable is a shortcut which is linked to this piece of data. 22 00:01:46,350 --> 00:01:52,800 So whenever you access the variable, it will access this piece of data in an algebraic way. 23 00:01:54,060 --> 00:01:59,520 So you can do them both separately or together, you could just do them by themselves where you declare 24 00:01:59,910 --> 00:02:06,270 and do the assignment later, or what typically has happened is you do both at the same time. 25 00:02:06,510 --> 00:02:10,970 So you would say the variable keyword denote the name of the variable. 26 00:02:11,160 --> 00:02:13,830 Use the assignment operator and then. 27 00:02:15,020 --> 00:02:15,920 Do the assignment. 28 00:02:18,200 --> 00:02:19,370 So let's go look at our code. 29 00:02:23,240 --> 00:02:26,360 OK, so let's show you an example of how we do this. 30 00:02:27,550 --> 00:02:28,780 Variable operation. 31 00:02:29,870 --> 00:02:34,580 We're going to start with declarations, so I'm just going to start with a let. 32 00:02:35,670 --> 00:02:40,230 And say, let me count equals zero. 33 00:02:40,710 --> 00:02:45,480 OK, so alternately, I could have started with just let count 34 00:02:48,660 --> 00:02:52,830 and then assign count equals zero. 35 00:02:54,750 --> 00:02:56,910 Let count, count equals zero. 36 00:02:57,420 --> 00:03:00,810 OK, so this is a declaration 37 00:03:04,020 --> 00:03:05,760 and this is an assignment. 38 00:03:10,020 --> 00:03:16,260 OK, if you want to do them at the same time, let's do a separate one. 39 00:03:17,130 --> 00:03:25,500 You could do a declaration and an assignment in the same stroke declaration and assignment. 40 00:03:26,560 --> 00:03:34,620 OK, the declaration comes from declaring the variable by stating the variable keyword and then the 41 00:03:34,620 --> 00:03:35,490 name of the variable. 42 00:03:36,120 --> 00:03:40,140 The assignment comes from using the assignment operator and giving it a value. 43 00:03:41,310 --> 00:03:43,500 So if I want, I can set that one to one. 44 00:03:43,680 --> 00:03:50,730 Now we have the difference between the two of them and I can access my variables. 45 00:03:55,340 --> 00:04:00,060 OK, so let's save that and see how this looks. 46 00:04:02,250 --> 00:04:03,300 Zero and one. 47 00:04:03,690 --> 00:04:07,560 OK, zoom in on our console so we can see that. 48 00:04:09,820 --> 00:04:18,070 All right, so that is declaration and assignment in JavaScript, and you can do the same thing, some 49 00:04:18,070 --> 00:04:23,440 other things you might need to understand is that you're going to run into some issues if you do this 50 00:04:23,440 --> 00:04:24,030 with CONSED. 51 00:04:29,650 --> 00:04:39,160 So if we say that just as is and give it a shot, there's a problem because cost is for constant variables 52 00:04:39,700 --> 00:04:43,210 and that means you cannot edit them after they have been declared. 53 00:04:44,440 --> 00:04:48,430 You need to initialize a const variable with a value. 54 00:04:49,720 --> 00:04:55,390 So you need to give this some kind of a value to start out with, and it's going to keep that same value. 55 00:04:56,260 --> 00:04:59,340 And you're not going to be able to edit that value because it's a concept. 56 00:04:59,350 --> 00:05:06,850 And the purpose of const is precisely that is to have a variable where you set the value once and then 57 00:05:06,970 --> 00:05:11,450 the code will not let you reset that value later, OK? 58 00:05:13,420 --> 00:05:15,130 But as you can see now, we can. 59 00:05:17,680 --> 00:05:24,490 Another thing you can note is that you cannot access count three before it's been initialized, so let's 60 00:05:24,490 --> 00:05:25,960 move the console lock down. 61 00:05:33,870 --> 00:05:39,300 Remember, you can hoist with far, but you cannot hoist with Consed, so one zero one and two. 62 00:05:39,330 --> 00:05:39,790 There you go. 63 00:05:41,490 --> 00:05:42,270 And we've got them. 64 00:05:43,470 --> 00:05:47,890 OK, and of course, you can do the same thing with Hvar. 65 00:05:49,110 --> 00:06:00,180 Now, the tricky the funny thing about far is that you could actually just declare a variable and reassign 66 00:06:00,180 --> 00:06:00,390 it. 67 00:06:05,150 --> 00:06:14,270 And this is by default, going to be a var, so it's the same as if you had the VA keyboard, so the 68 00:06:14,270 --> 00:06:25,700 default behavior for variables in JavaScript is VA and that's why we have to specify if it's a letter 69 00:06:25,700 --> 00:06:26,480 or a const. 70 00:06:27,320 --> 00:06:34,160 OK, so we save that refresh count for is not defined. 71 00:06:37,540 --> 00:06:38,560 So let's do that again. 72 00:06:41,970 --> 00:06:43,530 OK, let's see that. 73 00:06:49,220 --> 00:06:50,750 Let's get rid of that one. 74 00:06:53,210 --> 00:06:55,790 All right, so you can see where that's being defined. 75 00:06:59,970 --> 00:07:05,580 OK, so console log, count one, count two, count three and count four, in this case, this is a 76 00:07:05,580 --> 00:07:06,000 var. 77 00:07:08,690 --> 00:07:12,800 And so it'll behave the same exact way as we had before. 78 00:07:13,610 --> 00:07:17,330 That's being specified as a bar and if you don't specify anything, it's still a bar. 79 00:07:18,500 --> 00:07:24,290 OK, so that is our assignment for our lesson for declaration and assignment. 80 00:07:25,160 --> 00:07:26,750 Now we go on to lesson six.