All language subtitles for 5. Function Scope

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
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
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian Download
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: WEBVTT 00:00.300 --> 00:05.160 In this video I want to talk about scope in javascript as it relates to functions. 00:05.160 --> 00:10.290 Now you've already explored a scope as it relates to if statements we messed around with that in the 00:10.290 --> 00:11.630 basics directory. 00:11.640 --> 00:13.070 Here we have scoped out J. 00:13.090 --> 00:19.770 S. We learned that in javascript we have a lexical scoping we have a single global scope with multiple 00:19.830 --> 00:21.160 local scopes. 00:21.270 --> 00:27.120 And we learned that one way to create a new local scope is via the if statement so variables defined 00:27.120 --> 00:32.190 in the if statement are only accessible in there they're not accessible outside of there. 00:32.250 --> 00:36.790 And we explored how we can create a little visualization of just this. 00:36.810 --> 00:40.590 Now we're going to take the same rules and apply them to functions. 00:40.590 --> 00:44.310 So what I want to do is just create a new file in the function's directory. 00:44.310 --> 00:47.690 I'm going to call this one function hyphen scope. 00:47.730 --> 00:48.110 J. 00:48.150 --> 00:55.470 S And what we're going to do is copy and paste a function that we've already created in functions 101. 00:55.470 --> 01:00.150 We created convert Fahrenheit to Celsius for the challenge. 01:00.150 --> 01:03.390 Now your name might be slightly different since it was a challenge. 01:03.390 --> 01:06.680 I'm going to go ahead though and take all of the code down here. 01:06.750 --> 01:13.320 Copy it to the clipboard and paste it into function scope and we'll use this to explore how scope works 01:13.320 --> 01:15.380 with functions in Javascript. 01:15.390 --> 01:21.120 So let's go ahead and create a little scope tree to visualize the scope we have for this file. 01:21.120 --> 01:26.610 First up we got our global scope which we always have global scope and we have a few different things 01:26.610 --> 01:27.930 in the global scope. 01:28.020 --> 01:34.320 I have a convert Fahrenheit to Celsius. 01:34.590 --> 01:41.490 After that one I have temp one and I also have temp 2 now. 01:41.530 --> 01:45.540 Inside of that global scope we have a single local scope. 01:45.540 --> 01:53.070 Our functions define a new local scope this scope contains everything created in the code block. 01:53.130 --> 01:59.750 So that would include the Celcius variable but it also includes the function arguments. 01:59.760 --> 02:07.980 So in this case we would have a local scope and this local scope would have a Fahrenheit which is the 02:07.980 --> 02:12.480 function argument and it would have Celsius something defined in the function itself. 02:12.480 --> 02:19.290 This means that we can access Fahrenheit or Celsius outside of the function even after it runs. 02:19.290 --> 02:25.660 So right here I cannot access Fahrenheit I can not access Celsius they are not in scope. 02:25.740 --> 02:31.270 All I can access at this point would be convert Fahrenheit to Celsius temp one or temp two. 02:31.620 --> 02:36.540 So this is the exact same thing we saw with if statements the scope tree gets generated in much the 02:36.540 --> 02:37.360 same way. 02:37.470 --> 02:42.570 We just have a function as opposed to an if statement and the only difference that you really need to 02:42.570 --> 02:48.080 remember is that yes the arguments of a function are also bound to that local scope even though they're 02:48.090 --> 02:51.870 not defined inside of the curly braces. 02:51.870 --> 02:57.570 Now we could add one more thing to this example to make it a bit more real world let's say inside of 02:57.570 --> 02:58.230 the function. 02:58.230 --> 03:04.560 I want to add an if statement if I want to check if Celsius is less than or equal to zero which is the 03:04.560 --> 03:08.710 freezing point in Celsius if it is less than or equal to zero. 03:08.820 --> 03:12.840 I could say the left is freezing equal. 03:12.840 --> 03:13.770 True. 03:13.800 --> 03:16.810 Now in this example what's the scope tree look like. 03:16.830 --> 03:20.800 Well we have a new local scope in our existing local scope. 03:20.800 --> 03:24.400 So I'm going to indent local scope. 03:24.660 --> 03:26.580 And what do we have in the local scope. 03:26.580 --> 03:30.120 We have a single thing is freezing. 03:30.120 --> 03:36.150 So for example we would have the following scope tree inside of this if block we can access everything 03:36.330 --> 03:42.780 because we are either accessing is freezing or we're accessing something from one of the parents. 03:42.780 --> 03:48.570 That means I can access Fahrenheit and Celsius I can access our convert function temple on and temp 03:48.760 --> 03:49.620 too. 03:49.740 --> 03:56.010 So functions function much the same way as if statements when it comes to scope in javascript. 03:56.010 --> 04:00.970 Just because I create something in a function doesn't mean it's accessible outside of a function. 04:01.200 --> 04:04.380 And we're going to explore a more situations where this actually comes up. 04:04.440 --> 04:09.510 For now you just need to keep this in mind that functions create a local scope much like if statements 04:09.510 --> 04:14.940 do and function arguments are bound to that newly created local scope. 04:14.940 --> 04:17.790 And we add a little tree right here to prove it. 04:17.820 --> 04:18.170 All right. 04:18.180 --> 04:19.080 That's it for this video. 04:19.080 --> 04:20.640 Not a whole lot to discuss. 04:20.640 --> 04:25.010 I just wanted to make sure you are aware that functions create a new scope. 04:25.110 --> 04:28.420 We're going to continue on and get back to programming in the next one. 04:28.440 --> 04:31.220 I'm excited to get to it so I'll see you then. 5832

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