1 00:00:01,350 --> 00:00:05,939 Welcome back! Up to this point we've played around with some JavaScript types. 2 00:00:06,040 --> 00:00:09,250 Only 3 out of 7. Don't worry, we'll get to the rest of them. 3 00:00:09,400 --> 00:00:12,040 But there's one problem. 4 00:00:12,040 --> 00:00:15,070 How does the program remember things? 5 00:00:16,050 --> 00:00:25,770 For example, if we write "These pretzels are making me thirsty". 6 00:00:26,850 --> 00:00:28,350 And we'll do this. 7 00:00:28,350 --> 00:00:30,350 "+" We need the exclamation "!". 8 00:00:30,390 --> 00:00:31,040 OK. 9 00:00:31,620 --> 00:00:32,540 I enter that. 10 00:00:32,700 --> 00:00:34,100 OK. I get that. That's - that's good. 11 00:00:34,110 --> 00:00:36,750 But if I need to use this again. 12 00:00:36,940 --> 00:00:42,300 Well. Again I have to write "These pretzels 13 00:00:44,630 --> 00:00:47,980 are making me thirsty". 14 00:00:49,060 --> 00:00:52,870 And imagine if have to do that hundreds of times. 15 00:00:53,020 --> 00:00:55,160 That's kind of annoying right? 16 00:00:55,180 --> 00:01:00,560 That's - computers are supposed to help us but instead we just keep writing stuff and keep writing stuff. 17 00:01:00,580 --> 00:01:02,990 And that's not very efficient, is it? 18 00:01:04,069 --> 00:01:07,100 Well to catch and hold values 19 00:01:07,100 --> 00:01:15,630 JavaScript has something called variable's. And variables can be used 20 00:01:16,880 --> 00:01:18,870 with the word 'var'. 21 00:01:19,200 --> 00:01:34,590 So now if I go 'var george', so 'var george = "These pretzels are making me thirsty"' 22 00:01:35,620 --> 00:01:36,580 plus [+] "!". 23 00:01:39,900 --> 00:01:44,470 Notice the 'semicolon' here - I'll explain what that does in a bit. 24 00:01:44,520 --> 00:01:46,850 I press 'Enter'. I get 'undefined'. 25 00:01:46,850 --> 00:01:48,140 Let's not worry about that. 26 00:01:48,210 --> 00:01:49,610 That doesn't matter for now. 27 00:01:49,800 --> 00:01:57,000 And now if I go 'george' and you'll see that Google Chrome actually helps me and recognizes it. 28 00:01:57,090 --> 00:01:58,260 I press 'Enter'. 29 00:01:58,650 --> 00:02:03,250 "These pretzels are making me thirsty!". How awesome is that? 30 00:02:03,960 --> 00:02:06,250 So we're able to store this value. 31 00:02:07,110 --> 00:02:12,510 And obviously this is simple, but imagine, this was a long calculation that we had to make and we want 32 00:02:12,510 --> 00:02:13,330 to access it. 33 00:02:13,380 --> 00:02:17,180 Well, all we need to do now is have 'george'. 34 00:02:17,190 --> 00:02:18,840 It's a 'variable'. 35 00:02:18,870 --> 00:02:25,490 So now you see in the last video when I said that when we're comparing things we have to use '===' 36 00:02:25,850 --> 00:02:34,850 Well because when you do '3=3' it's saying: 'Assign the number 3 to 3'. 37 00:02:35,100 --> 00:02:38,280 But there's a rule with variables. With variables 38 00:02:38,420 --> 00:02:47,180 you can't start with numbers. So if I go 'var 3 = 3', I'll get an error. 39 00:02:47,360 --> 00:02:53,790 But if I go 'var three = 3', well, that's no problem. 40 00:02:54,200 --> 00:02:57,960 And variables have a few rules. 41 00:02:58,910 --> 00:03:03,710 The rules are: Well, it needs to start with a letter. 42 00:03:03,710 --> 00:03:11,550 It can end with a number, that's fine, but it needs to start with the letter and it can't start with a symbol. 43 00:03:11,660 --> 00:03:22,030 So I can do 'var &hello = 5'. Now I'll get en error. 44 00:03:22,040 --> 00:03:28,480 So a variable needs to start with the letter, can end in a number, that's fine. 45 00:03:28,700 --> 00:03:37,140 And there's a few other special cases. A variable can also start with a '$' or '_'. 46 00:03:37,520 --> 00:03:42,950 But we don't need to worry about that too much and the standard and JavaScript so you won't get an 47 00:03:42,950 --> 00:03:43,400 error. 48 00:03:43,430 --> 00:03:52,610 If this happens, but you do something called 'camel case'. So if it was a word, let's say 'firstname', you 49 00:03:52,710 --> 00:03:56,660 do 'camel case', because these are two separate words. You do lowercase 50 00:03:56,690 --> 00:04:01,120 the first word. Second word is going to have uppercase. 51 00:04:01,160 --> 00:04:08,200 So 'firstName'. You've capitalized the first letter and that's called 'camel case'. 52 00:04:08,200 --> 00:04:16,029 So still not getting what variables are? Well, I'm going to demonstrate this point further 53 00:04:16,029 --> 00:04:20,620 and why we need to store values in a programming language. 54 00:04:20,620 --> 00:04:28,210 I'm going to introduce you to a little cool trick called 'prompt'. And 'prompt()', when I use these brackets 55 00:04:29,710 --> 00:04:30,920 and I press 'Enter'... 56 00:04:31,570 --> 00:04:32,520 Look at that. 57 00:04:32,630 --> 00:04:37,470 I've got something here and it's asking for something and I press OK. 58 00:04:37,610 --> 00:04:38,790 And look at that! 59 00:04:38,930 --> 00:04:40,380 It returns whatever I typed. 60 00:04:40,550 --> 00:04:42,680 Let's try that again. If I go 'prompt()', 61 00:04:43,700 --> 00:04:47,030 press 'Enter' and say 'Hello', press 'OK'. 62 00:04:48,240 --> 00:04:49,840 I get that response back. 63 00:04:50,210 --> 00:04:50,420 OK. 64 00:04:50,430 --> 00:04:55,430 So how can we say something like 'What's your username?' and store that value? 65 00:04:55,470 --> 00:05:02,790 That's something that we use quite a lot on websites, right? Well, with 'prompt' you can do 'prompt()'. 66 00:05:02,790 --> 00:05:11,930 Then here we'll say "What is your username". 67 00:05:12,000 --> 00:05:13,890 So we're entering the string here. 68 00:05:14,160 --> 00:05:17,380 And if I press 'Enter'. 69 00:05:17,510 --> 00:05:20,000 OK, so it says "What is your username". 70 00:05:20,000 --> 00:05:28,880 So now if I type in my username is "andrei", I get the 'username'. So using that knowledge, 71 00:05:29,080 --> 00:05:33,940 let's make a calculator. Let's do 'var first = prompt()' 72 00:05:38,420 --> 00:05:40,310 "enter first number" 73 00:05:43,120 --> 00:05:49,840 And then I 'Enter' pressed 'Shift' here, so that I can add a new line. So 'Shift', 'Enter' and I'm going to say 74 00:05:50,680 --> 00:05:54,840 'var second = prompt()' 75 00:05:58,060 --> 00:06:00,710 "enter second number" 76 00:06:04,550 --> 00:06:14,580 Now if I press 'Enter', I'll get 'enter first number'. I'll say '5', press OK, then 'enter second number' and I 77 00:06:14,580 --> 00:06:17,460 say '10' and press OK. 78 00:06:19,210 --> 00:06:19,430 OK. 79 00:06:19,450 --> 00:06:21,970 But now I got 'undefined'. 80 00:06:22,330 --> 00:06:23,730 But here's the cool thing. 81 00:06:23,750 --> 00:06:31,470 Now I have these values held in these variables, so I can use them. If I check 'first', 82 00:06:31,510 --> 00:06:35,260 I have "5"; if I check 'second', 83 00:06:35,480 --> 00:06:37,080 I have "10". 84 00:06:37,340 --> 00:06:39,810 But you'll notice that I have double quotes [""] around them. 85 00:06:39,980 --> 00:06:46,230 And that's because 'prompt' automatically changes it to String, it's expecting a form of text. 86 00:06:46,280 --> 00:06:50,540 So there's a trick to change a String into a Number. 87 00:06:50,730 --> 00:06:56,960 And all we need to do and this is just syntax we need to remember is 'Number(first)'. 88 00:06:59,640 --> 00:07:00,730 And it'll give us '5'. 89 00:07:00,900 --> 00:07:21,280 So all we need to do now is do 'Number(first) + Number(second)'. And we'll get the sum of these 2. 90 00:07:21,310 --> 00:07:23,530 But again we can use a variable here. 91 00:07:23,530 --> 00:07:25,260 I don't want to keep typing this. 92 00:07:25,270 --> 00:07:33,400 So what if I do 'var sum = Number(first) + Number(second);' 93 00:07:33,640 --> 00:07:39,340 Press 'Enter'. Nothing yet, because it's in the 'sum' now. 94 00:07:40,740 --> 00:07:45,410 So if I asked for 'sum, I get '15'. 95 00:07:45,510 --> 00:07:45,720 OK. 96 00:07:45,720 --> 00:07:50,340 So let's write our first program here. 97 00:07:50,520 --> 00:07:54,800 I'm going to show you one other cool trick and that is 'alert'. 98 00:07:54,810 --> 00:08:01,710 So instead of 'prompt' we're going to use 'alert'. And 'alert()'. 99 00:08:01,720 --> 00:08:04,600 What it does is - it says "hi there!" 100 00:08:08,330 --> 00:08:14,510 If I press 'Enter', I get a pop-up, but without anything to enter, just an 'OK' button. 101 00:08:14,530 --> 00:08:18,550 So using this we can create a calculator. 102 00:08:18,730 --> 00:08:19,870 So let's do this.