1 00:00:01,050 --> 00:00:06,570 Welcome back in today's lecture, are we going to talk about JavaScript variables we are going to see 2 00:00:06,570 --> 00:00:14,310 about variables, alert function, prompt function and undefined data types or JavaScript types? 3 00:00:14,530 --> 00:00:16,680 Remember, that was from the first lecture. 4 00:00:16,890 --> 00:00:21,240 And I've told you that we need to do this later in the course. 5 00:00:21,600 --> 00:00:32,610 OK, so open your vehicle, start new lecture, call it lecture, dash three JS. 6 00:00:33,000 --> 00:00:35,870 Make sure that you link it here first. 7 00:00:35,880 --> 00:00:36,810 Copy this one. 8 00:00:38,500 --> 00:00:45,140 OK, then we need to comment it out and make sure that you changed it to three. 9 00:00:46,240 --> 00:00:50,760 OK, now our browser should be like this. 10 00:00:51,280 --> 00:00:52,960 Actually, we don't need that one. 11 00:00:53,080 --> 00:00:53,470 No. 12 00:00:54,700 --> 00:00:59,330 Variables are something that is very crucial for JavaScript. 13 00:01:00,100 --> 00:01:03,780 So let us to store values inside this variable. 14 00:01:03,790 --> 00:01:08,800 So consider them as a container so we can add some data, we can store something inside. 15 00:01:09,250 --> 00:01:14,760 So this way, we are giving the program a memory where we can store data. 16 00:01:15,160 --> 00:01:16,750 So variables hold. 17 00:01:17,640 --> 00:01:24,120 Any JavaScript data type so they can hold any data that we have just covered. 18 00:01:24,810 --> 00:01:28,860 OK, so let's call it here variables. 19 00:01:30,420 --> 00:01:40,050 JavaScript variables helps with having variables, and so first things first. 20 00:01:40,940 --> 00:01:47,030 We need to first specify that we are talking about the variables and how are we going to do this, we 21 00:01:47,590 --> 00:01:48,860 we are. 22 00:01:49,160 --> 00:01:55,190 And then there is goes the name of the variable and there we can specify the data. 23 00:01:55,550 --> 00:01:56,510 And now this. 24 00:01:57,030 --> 00:02:02,390 Another operator comes to place equals means we are assigning data here. 25 00:02:02,780 --> 00:02:03,140 All right. 26 00:02:04,490 --> 00:02:09,380 So how we can print out the variables to consider that log name. 27 00:02:11,360 --> 00:02:18,050 Let's call here my name and take notes, something that I did here and I will just explain a little 28 00:02:18,050 --> 00:02:18,570 bit later. 29 00:02:18,800 --> 00:02:21,860 So go back to your browser and here it is. 30 00:02:24,600 --> 00:02:32,040 Let's change it to more meaningful, Rick, or you can put Andy doesn't matter your name, so why variables 31 00:02:32,040 --> 00:02:33,360 are so useful. 32 00:02:33,820 --> 00:02:35,700 OK, let's do it like this. 33 00:02:35,910 --> 00:02:39,840 And I'm going to show you, First Consul, that Locke. 34 00:02:41,070 --> 00:02:50,070 Five plus nine and save it, so that will give us a result 14, but if I want to access the same result, 35 00:02:50,400 --> 00:02:56,430 I need to copy the same line of code and execute it here. 36 00:02:56,760 --> 00:02:57,090 Right. 37 00:02:57,300 --> 00:02:59,570 So that will give me to fourteens. 38 00:03:00,450 --> 00:03:01,750 Well, this is not ideal. 39 00:03:01,770 --> 00:03:03,540 This is called code repetition. 40 00:03:03,810 --> 00:03:07,290 And there is a rule here in not just in JavaScript. 41 00:03:08,220 --> 00:03:11,370 Or any other programming languages, it's called dry. 42 00:03:11,490 --> 00:03:18,960 Do not repeat yourself, so it is a good idea to find a solution how we will not be able to repeat ourselves. 43 00:03:19,230 --> 00:03:22,590 So that's where variables comes into place. 44 00:03:22,950 --> 00:03:28,640 Now, what we can do here, I'm just going to recommend this one, for example, variable. 45 00:03:29,190 --> 00:03:33,560 Let's put it here somewhere and I will put five plus nine. 46 00:03:33,900 --> 00:03:37,520 OK, now if I go back here, there is nothing there, right? 47 00:03:37,710 --> 00:03:46,740 But I could call it log just some because the very variable, some holes in the memory, this data 14. 48 00:03:47,160 --> 00:03:47,540 Right. 49 00:03:47,940 --> 00:03:56,760 And if I create another variable called result, then I can still use this some some minus five for 50 00:03:56,760 --> 00:03:57,240 example. 51 00:03:57,420 --> 00:03:58,620 And that should give me. 52 00:03:59,940 --> 00:04:00,390 Nine. 53 00:04:01,520 --> 00:04:05,430 And I need to also consulate like the result. 54 00:04:05,780 --> 00:04:12,170 So now you see the power of variables, we can we use them because they're dear, they're defined, 55 00:04:12,410 --> 00:04:14,810 they're they have a value. 56 00:04:15,170 --> 00:04:22,130 And now we can use them in the course of if I want, I can just again use the result and I say. 57 00:04:23,970 --> 00:04:24,870 Let's make it here. 58 00:04:24,900 --> 00:04:30,690 This is the result from the variable. 59 00:04:32,540 --> 00:04:39,410 Semicolon there, plus make a breathing space, make sure that you close the semicolon and there you 60 00:04:39,410 --> 00:04:39,690 go. 61 00:04:39,860 --> 00:04:41,350 Now I have nine again. 62 00:04:41,360 --> 00:04:43,760 I have nine here so I can reuse it. 63 00:04:43,790 --> 00:04:45,250 That's called reusability. 64 00:04:45,740 --> 00:04:47,170 So that is pretty much it. 65 00:04:48,270 --> 00:04:56,110 And now let's talk about the part here, the notation, the way we are just giving the variable names. 66 00:04:56,350 --> 00:05:03,330 Now, it is very important for us as a developers to create good variable names. 67 00:05:03,660 --> 00:05:10,890 A variable should accurately identify a variable name, actually should accurately and identify your 68 00:05:10,890 --> 00:05:11,520 variable. 69 00:05:11,850 --> 00:05:12,180 Right. 70 00:05:12,210 --> 00:05:19,320 For example, if I say my name, then you should as a developer from outside to expect that, well, 71 00:05:19,320 --> 00:05:21,390 this will hold a string with my name. 72 00:05:21,540 --> 00:05:21,870 Right. 73 00:05:22,170 --> 00:05:23,760 But what about this notation? 74 00:05:23,760 --> 00:05:28,830 It's going to call it's going to be called cavalcades notation in how this notation works. 75 00:05:29,100 --> 00:05:35,940 Now, when we have a variable that is built from two words like first. 76 00:05:37,130 --> 00:05:43,500 Name, then we are going to use the Campbell case notation and how this Camil case notation works. 77 00:05:43,730 --> 00:05:45,500 Now, the Kemakeza notation works. 78 00:05:45,740 --> 00:05:51,620 The first word is going to stay with a small first letter. 79 00:05:51,620 --> 00:05:53,390 So it's not going to be capital letter. 80 00:05:53,690 --> 00:05:59,370 But the second word will have a capital first letter and it will be this first name. 81 00:05:59,630 --> 00:06:05,420 So throughout the course, I will use Camil case notation and you should also do the same thing, but 82 00:06:05,600 --> 00:06:06,350 in future. 83 00:06:07,300 --> 00:06:15,400 If you see somewhere or in some drills that they use some other notation, then that is OK, it's going 84 00:06:15,400 --> 00:06:15,700 to work. 85 00:06:16,230 --> 00:06:25,300 Now, another rule, how we can name our variables is that the very variable names cannot contain spaces. 86 00:06:25,540 --> 00:06:30,690 For example, variable, my name, it's not going to work. 87 00:06:30,700 --> 00:06:31,980 It's contained spaces. 88 00:06:32,200 --> 00:06:37,840 Another thing that the variable you should know that they must start with a letter on their score so 89 00:06:37,840 --> 00:06:43,060 they can start with the underscore and you can continue with combinations of letters and numbers. 90 00:06:43,330 --> 00:06:43,680 Right. 91 00:06:43,810 --> 00:06:45,100 So you can even have a numbers. 92 00:06:45,320 --> 00:06:46,570 You can start with underscore. 93 00:06:46,570 --> 00:06:50,360 We can start with the letter or you can start with a dollar sign. 94 00:06:50,860 --> 00:07:02,020 So let's do it like this, where names can start with letter underscore or. 95 00:07:03,560 --> 00:07:07,110 Dollar sign, right, that is pretty much it. 96 00:07:07,280 --> 00:07:12,950 Now, another thing is you should know that variables are case sensitive. 97 00:07:14,510 --> 00:07:18,500 What that means, variable A B equals. 98 00:07:19,970 --> 00:07:20,510 Eight. 99 00:07:22,380 --> 00:07:29,520 And variable A B equals eight are very different, why they're case sensitive, this is a capital B. 100 00:07:29,820 --> 00:07:34,820 This is a lower B, so they're D variable is completely different than this. 101 00:07:35,070 --> 00:07:41,070 Now, in the memory, we have two different variables, but they also have same value. 102 00:07:41,160 --> 00:07:42,510 But that's different. 103 00:07:43,020 --> 00:07:48,320 And yeah, so variable can contain letters, numbers underscore or dollar signs. 104 00:07:48,330 --> 00:07:52,340 They're case sensitive and they can start with this three. 105 00:07:53,340 --> 00:07:58,380 So you cannot start variable with one or you cannot start variable with a special symbol like this. 106 00:07:59,040 --> 00:08:04,730 And that or even the special symbol will not be able to be applied here. 107 00:08:04,740 --> 00:08:09,390 So if you save it like this, save it, you will have wait. 108 00:08:09,750 --> 00:08:12,000 You'll have unexpected token. 109 00:08:12,660 --> 00:08:14,670 And that was the token that we just put. 110 00:08:15,660 --> 00:08:16,240 Nice. 111 00:08:16,470 --> 00:08:20,180 So let's continue working with our examples. 112 00:08:20,460 --> 00:08:27,960 So what I want to show you that for the first time we will be able to use a function. 113 00:08:28,230 --> 00:08:30,300 So these functions are inbuilt. 114 00:08:30,540 --> 00:08:32,490 So we have access here. 115 00:08:32,790 --> 00:08:39,480 For example, the first is prompt and if I do it like this, F stands for function. 116 00:08:39,480 --> 00:08:41,880 So it tells me that is a function. 117 00:08:42,090 --> 00:08:47,160 But every time I use function, I need to explicitly write these brackets. 118 00:08:47,490 --> 00:08:48,800 So cancel. 119 00:08:48,810 --> 00:08:53,550 Actually I don't need to cancel the log now in semicolon again. 120 00:08:53,580 --> 00:09:00,210 Now if I go back to a browser then the browser will give me a pop up where I can put a data or string 121 00:09:00,210 --> 00:09:00,600 here. 122 00:09:01,020 --> 00:09:05,970 And if he did, OK, now there will be stored somewhere. 123 00:09:08,100 --> 00:09:08,700 Prompt. 124 00:09:10,920 --> 00:09:15,190 So, again, I will not be able to access the data from the program. 125 00:09:15,450 --> 00:09:16,050 Why is that? 126 00:09:16,320 --> 00:09:22,260 Well, because I have in store it in variable, but what I can do is variable data. 127 00:09:23,190 --> 00:09:34,590 That then I haven't told you this, you can write here, what is your name and make sure that this I's 128 00:09:34,590 --> 00:09:36,110 capital looks better. 129 00:09:36,960 --> 00:09:40,170 Now look what will happen here is what is your name? 130 00:09:40,170 --> 00:09:42,060 But the browser is still there. 131 00:09:42,270 --> 00:09:48,950 So, Rick, now, if I hit OK or return, then again, I don't have access. 132 00:09:49,110 --> 00:09:50,320 No, I'm just joking. 133 00:09:50,520 --> 00:09:56,400 So the data variable is here and yeah, I just have access to whatever. 134 00:09:56,400 --> 00:09:58,050 We just enter through that prompt. 135 00:09:58,440 --> 00:10:02,510 So why I just access that data here and then right here. 136 00:10:02,520 --> 00:10:05,500 Console the log in that data. 137 00:10:05,520 --> 00:10:08,430 Well if I do this in I will save it. 138 00:10:08,700 --> 00:10:14,030 Then the entire life server will kick in every other line. 139 00:10:14,040 --> 00:10:18,960 It will go line by line, the compiler and it will delete whatever had previously. 140 00:10:18,960 --> 00:10:21,410 Hold it here, store it here in this data. 141 00:10:22,680 --> 00:10:29,040 That's why sometimes it's very useful, this browser console, so I can just use it. 142 00:10:29,520 --> 00:10:30,370 That's pretty much it. 143 00:10:30,600 --> 00:10:32,460 Now let's do something interesting. 144 00:10:33,810 --> 00:10:41,150 Another function that I want to show you is called alert, but let's spice this one up. 145 00:10:41,160 --> 00:10:45,090 So I'm just going to prompt the user and I will say, how old are you? 146 00:10:47,240 --> 00:10:56,180 Are you and I'm just going to copy this page that now I'm just going to ask about the year and what 147 00:10:56,180 --> 00:10:56,940 I'm going to say. 148 00:10:58,460 --> 00:11:02,120 What is the current? 149 00:11:04,570 --> 00:11:09,680 You're nice and I'm going to use a learned function for the first time. 150 00:11:09,700 --> 00:11:14,560 Now, this alert, I don't have to save it in a very old I can, but I don't have to. 151 00:11:14,830 --> 00:11:17,440 And I will say I was born. 152 00:11:19,910 --> 00:11:28,310 And and semicolon here, space it plus now I don't want to confuse this function, what I will do, 153 00:11:28,640 --> 00:11:36,380 I will get this in a brackets and I will just minus a year with the H. 154 00:11:36,530 --> 00:11:38,810 So why just go with the year will here? 155 00:11:38,810 --> 00:11:40,730 It will be bigger than the H obviously. 156 00:11:40,910 --> 00:11:43,200 And that will never produce a negative result. 157 00:11:43,340 --> 00:11:44,510 So this is a pure mess. 158 00:11:44,870 --> 00:11:45,610 So save it. 159 00:11:45,620 --> 00:11:46,820 Go back to your browser. 160 00:11:47,700 --> 00:11:48,500 How old are you. 161 00:11:48,890 --> 00:11:49,520 Thirty three. 162 00:11:49,520 --> 00:11:50,900 I don't know if you can see this. 163 00:11:51,470 --> 00:11:51,680 How. 164 00:11:51,810 --> 00:11:53,670 Let me just zoom in if I can. 165 00:11:53,840 --> 00:11:54,690 I can actually. 166 00:11:54,860 --> 00:11:56,400 So thirty three here. 167 00:11:56,490 --> 00:11:57,770 OK then. 168 00:11:57,770 --> 00:12:00,600 Twenty, twenty two here. 169 00:12:00,620 --> 00:12:02,360 OK then. 170 00:12:02,360 --> 00:12:03,870 I was born in this year. 171 00:12:04,010 --> 00:12:04,700 Nice. 172 00:12:05,720 --> 00:12:12,020 That is pretty much what the alert function can do in there, out of the box there for us so we can 173 00:12:12,020 --> 00:12:12,510 use it. 174 00:12:12,980 --> 00:12:15,800 So this is to alert the user that something is happening. 175 00:12:15,810 --> 00:12:23,450 For example, you have you have failed to log in or your password and confirm password does not match. 176 00:12:23,660 --> 00:12:28,630 There are some scenarios that we can use all of this and they're pretty much cool. 177 00:12:28,760 --> 00:12:30,150 When I started JavaScript. 178 00:12:30,170 --> 00:12:37,580 I was I was like using this problem in my in my lectures and in my exercises. 179 00:12:37,580 --> 00:12:44,290 And I was pretty much, you know, excited because, oops, I am I'm doing something with JavaScript, 180 00:12:44,300 --> 00:12:45,710 so that's pretty much it. 181 00:12:46,400 --> 00:12:51,080 And the last thing that I just want to talk is the undefined. 182 00:12:52,990 --> 00:12:55,130 JavaScript is type. 183 00:12:55,810 --> 00:12:57,720 Now, this is from the first lecture. 184 00:12:59,300 --> 00:13:03,530 Right, we just get Boolean true, but we didn't finish with the rest of them. 185 00:13:03,760 --> 00:13:04,210 Why? 186 00:13:04,480 --> 00:13:07,660 Well, because, for example, if I do hear variable. 187 00:13:09,270 --> 00:13:17,760 Last name in semicolon and save it and try to conceal a log, last name, so I'm accessing the last 188 00:13:17,760 --> 00:13:26,870 name, but I didn't initialize the value, so maybe I haven't told you what variable my name is or variable. 189 00:13:26,880 --> 00:13:33,180 Any name here is saying, OK, we are going to declare variable with this name and we're going to assign 190 00:13:33,180 --> 00:13:34,230 value after that. 191 00:13:34,560 --> 00:13:36,090 So let me see. 192 00:13:36,090 --> 00:13:37,260 Let me show you what will happen. 193 00:13:38,220 --> 00:13:41,280 Oh, now it's a good idea to go back here. 194 00:13:42,390 --> 00:13:46,170 Just to comment out this part, because it will ask you the problems all over again. 195 00:13:46,250 --> 00:13:48,710 You don't want to see it, so it's going to say undefined. 196 00:13:48,720 --> 00:13:57,900 So why we are having undefined and this is one of the data JavaScript types that we need to discuss 197 00:13:58,170 --> 00:13:58,620 now. 198 00:13:58,890 --> 00:14:06,300 Undefined is meaning that we don't have any value in that variable. 199 00:14:06,300 --> 00:14:10,440 So undefined is that when we try to access something that doesn't hold data. 200 00:14:10,920 --> 00:14:17,810 So, yes, there is a memory triste of our variable called a last name, but there is no data. 201 00:14:17,940 --> 00:14:20,310 So it's only memory reference doesn't hold any data. 202 00:14:20,320 --> 00:14:25,800 And that's why when we try to access it here, it gives us this undefined. 203 00:14:26,040 --> 00:14:34,320 Now, when I haven't shown you is that you can also later on before you use this last name, you can 204 00:14:34,320 --> 00:14:40,440 say you can actually initialize you can give a data and now it will work. 205 00:14:40,440 --> 00:14:41,550 It will say Sokolsky. 206 00:14:41,550 --> 00:14:41,970 Why? 207 00:14:42,270 --> 00:14:49,920 Because some counseling log after I declare and initialize with the data this variable that is simple 208 00:14:49,920 --> 00:14:51,470 as it can be. 209 00:14:51,810 --> 00:14:54,120 Now, that is pretty much what I want to show you. 210 00:14:54,420 --> 00:14:59,380 And as a lasting, um, it's good idea to mention the semicolon. 211 00:14:59,670 --> 00:15:04,050 So one thing that we haven't covered is the semicolon that I put at the end. 212 00:15:04,290 --> 00:15:06,990 So semicolon marks the end of the expression. 213 00:15:07,260 --> 00:15:13,320 Anything that is written as a value like this is considered as expression. 214 00:15:13,350 --> 00:15:17,010 Now, it will be working without Collo nowadays, but semicolon. 215 00:15:17,980 --> 00:15:19,950 So it will be working even without semicolon. 216 00:15:19,950 --> 00:15:21,560 But it should also. 217 00:15:22,010 --> 00:15:29,730 Very good point for you to just start using the semicolon because that will end the expression. 218 00:15:29,730 --> 00:15:38,430 And yeah, it's a very useful MI, as is a teacher, to teach you that you should use it and that will 219 00:15:38,430 --> 00:15:39,030 stay with you. 220 00:15:39,030 --> 00:15:43,800 And you can later on in your programming you will use semicolons. 221 00:15:43,830 --> 00:15:45,060 So that is pretty much it. 222 00:15:45,060 --> 00:15:51,750 Now semicolon is anything that is written as a value, is considered as expression and semicolons puts 223 00:15:52,590 --> 00:15:55,080 always at the end of that expression. 224 00:15:55,560 --> 00:15:57,210 And that is pretty much for this lecture. 225 00:15:57,210 --> 00:16:02,080 We haven't done much and you can come at this one now, but it is what it is. 226 00:16:02,250 --> 00:16:06,120 Make sure that you do the exercises, so I will see you in the next one.