All language subtitles for 5. Variables

af Afrikaans
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:01,350 --> 00:00:05,940 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,070 --> 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,030 So still not getting what variables are? Well, I'm going to demonstrate this point further 53 00:04:16,030 --> 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. 103 00:08:19,900 --> 00:08:25,960 And in Chrome if you actually press the '↑' you'll remember your history and you can pull up whatever 104 00:08:25,960 --> 00:08:27,080 you've typed in. 105 00:08:27,250 --> 00:08:31,410 So you remember here we have 'first', 'second'. 106 00:08:31,500 --> 00:08:47,420 So those are the two 'prompt'-s and then I'm going to say 'var sum = Number(first) +' 107 00:08:48,850 --> 00:08:54,200 'Number(second);' 108 00:08:54,230 --> 00:08:58,680 And finally again I do 'Shift', 'Enter', so that the code doesn't get executed. 109 00:08:58,880 --> 00:09:03,330 And I do 'alert(sum);'. 110 00:09:03,390 --> 00:09:06,570 Just looking at this, what do you think is going to happen? 111 00:09:06,580 --> 00:09:12,990 I first asked for a 'prompt' and I stored that value in 'first'; the second 'prompt' asked for a 'second' number. 112 00:09:13,030 --> 00:09:14,590 Stores it in the 'second'. 113 00:09:14,590 --> 00:09:18,840 The 'sum' calculates the two numbers and then I 'alert' the 'sum'. 114 00:09:19,030 --> 00:09:20,730 I'm going to press 'Enter' and see what happens. 115 00:09:22,460 --> 00:09:24,730 Enter first number. I'm going to say '15'. 116 00:09:25,160 --> 00:09:29,010 OK. Next number. I'm going to say '20'. 117 00:09:29,030 --> 00:09:30,460 OK. 118 00:09:30,510 --> 00:09:31,710 And look at that! 119 00:09:31,830 --> 00:09:36,460 I got '35' as the answer. 120 00:09:36,560 --> 00:09:42,660 And again, knowing what we know about Strings and Numbers, I can now do - 121 00:09:42,660 --> 00:09:46,750 again I pressed '↑' to have exactly what I wrote back, 122 00:09:46,770 --> 00:09:57,950 Now I can say within the 'alert("The sum is:" + sum);'. 123 00:09:58,010 --> 00:10:08,750 Let's see that. If I press 'Enter', I'll go '3 + 4'. 'The sum is:7'. Look at that! Our first program. 124 00:10:09,380 --> 00:10:18,720 Variables can hold any JavaScript type. Think of them as little drawers in your desk. 125 00:10:18,720 --> 00:10:25,830 So in your desk you can open up that drawer and put anything you want in there: your pens, paper, erasers, 126 00:10:25,830 --> 00:10:29,630 calculators and you can close that drawer. 127 00:10:29,670 --> 00:10:34,680 And whenever you need, a pen or a calculator, you can open that drawer and grab it. 128 00:10:34,680 --> 00:10:37,210 And that's what variables are. 129 00:10:37,400 --> 00:10:44,950 Now the one thing that I've kind of omitted and I haven't told you about is this little weird semicolon here. 130 00:10:44,990 --> 00:10:46,430 Why do we need it? 131 00:10:46,430 --> 00:10:55,640 I mean, we were able to do '3+4' and we didn't see a 'semicolon'. Well, in JavaScript 132 00:10:55,640 --> 00:11:00,930 'semicolon' means the end of an expression. 133 00:11:00,960 --> 00:11:02,870 So what does that mean? 134 00:11:03,860 --> 00:11:11,850 A fragment of code that produces a value is called an 'expression'. So any value that's written really 135 00:11:11,850 --> 00:11:12,550 is an 'expression'. 136 00:11:12,570 --> 00:11:20,910 So if I do '3', well, that's an 'expression', it's produces a value. '3+4' is an 'expression'. In 137 00:11:20,910 --> 00:11:21,930 JavasScript 138 00:11:21,990 --> 00:11:25,820 the rule is - and there are some people actually that don't use semicolons anymore. 139 00:11:25,830 --> 00:11:33,080 But the way it was built and the way that it's intended to work is to do '3+4' add a 'semicolon'. 140 00:11:33,270 --> 00:11:37,140 Most of the time if you forget, it will still work. 141 00:11:37,180 --> 00:11:39,950 There's only small cases where I'll break your program. 142 00:11:40,020 --> 00:11:47,010 But just to form good habits for now, at the end of each expression, so that is anything we've done so 143 00:11:47,010 --> 00:11:50,890 far, add a 'semicolon' and you'll get used to it. 144 00:11:52,710 --> 00:12:01,600 OK. Let's keep going with variables, because they're very interesting. What if I do 'var a = true'? 145 00:12:02,090 --> 00:12:06,060 Because remember variables can hold anything. 146 00:12:06,380 --> 00:12:15,500 So variables can hold Booleans, and variables can hold Strings, and variables can hold Numbers, as we've seen. 147 00:12:15,500 --> 00:12:17,610 So we're just going to hold 'var a = true'. 148 00:12:17,650 --> 00:12:22,170 But now what if I - 149 00:12:22,560 --> 00:12:27,360 well I can access 'a', but what if I say 'a = "Hello"?' 150 00:12:27,710 --> 00:12:35,300 What do you think will happen if I press 'a' and then 'Enter'? 151 00:12:35,300 --> 00:12:46,710 Well, I get "Hello", because you can reassign a value to 'a', because initially the program thought 'a' equals to 'true'. 152 00:12:46,720 --> 00:12:49,770 But then we said: 'Well, "a" = "Hello" now'. 153 00:12:49,810 --> 00:12:58,810 So it discarded the 'true' value and now 'a' is equal to 'Hello' and we've completely forgotten about 'true' 154 00:13:00,340 --> 00:13:01,040 OK. 155 00:13:01,390 --> 00:13:02,710 Another little trick. 156 00:13:02,770 --> 00:13:06,940 What happens if I go like this? [var a;] 157 00:13:06,940 --> 00:13:08,420 Does that work or will I get an error? 158 00:13:08,470 --> 00:13:11,740 Let's see. 159 00:13:11,890 --> 00:13:12,490 OK. 'undefined'. 160 00:13:12,490 --> 00:13:14,530 I didn't get that red bar, no error. 161 00:13:14,560 --> 00:13:18,010 OK. What if I access the 'a', let's see what happens. 162 00:13:18,010 --> 00:13:24,880 Well "Hello" was assigned at the last part. Let's do a different variable. So if I do variable 'b', which 163 00:13:24,880 --> 00:13:31,860 we haven't used before. And I do this. Let's see if we can access it. 'undefined'. 164 00:13:32,000 --> 00:13:44,110 'b' is undefined. And you may have noticed this before, but 'undefined' is our 4th type in JavaScript. And 165 00:13:44,110 --> 00:13:50,920 I know it's weird, because you're, you're asking yourself "What is 'undefined'?". And 'undefined' is just one 166 00:13:51,160 --> 00:13:54,630 thing in JavaScript. As the name suggests, 167 00:13:54,640 --> 00:13:57,600 this means that the variable has not been assigned. 168 00:13:58,090 --> 00:14:07,240 So, think of it as you'll get 'undefined' if variable 'password' is like this. 169 00:14:07,270 --> 00:14:15,760 So if a user doesn't put in a 'password', well, you'll get 'undefined' and you'll be able to say "Well 'password' 170 00:14:15,760 --> 00:14:16,750 is undefined". 171 00:14:16,810 --> 00:14:20,750 You need to enter a 'password' or maybe a form requires an email address. 172 00:14:20,750 --> 00:14:30,250 Well, if email is 'undefined', then you can't sign them up to the form. So 'undefined' is used when nothing 173 00:14:30,250 --> 00:14:33,750 is assigned to a variable. 174 00:14:33,770 --> 00:14:38,740 So remember this, just like you move stuff out of the drawer in your desk and put something else 175 00:14:38,740 --> 00:14:43,490 in, variables can be assigned to different things. 176 00:14:43,490 --> 00:14:51,730 I can move my pen from one drawer to another, I can change things in that drawer and if there's nothing 177 00:14:51,730 --> 00:14:52,560 in that drawer, 178 00:14:52,600 --> 00:14:56,400 well, it's 'undefined'. OK. 179 00:14:56,880 --> 00:14:59,370 That's a very-very big concept in JavaScript. 180 00:14:59,370 --> 00:15:04,100 Hopefully you understand. I have a few exercises for you at the end of this video. 181 00:15:04,110 --> 00:15:05,720 I know you can do it! 182 00:15:05,730 --> 00:15:08,870 Good luck! And see you in the next video. 183 00:15:08,940 --> 00:15:09,150 Bye-bye 16984

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.