text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
it's just mutated it.
3,744.63
1.82
On the other side, if we did something like let thisIsALet A Let equal to 51,
3,746.45
7.92
and then want to change that later, we go 50, that's totally OK.
3,754.37
6.04
But if I actually tried to reassign-- so let thisIsALet.
3,763.3
5.87
If I tried to do this again, it's going to yell at me because it's saying, hey,
3,769.17
3.78
you already declared something called thisIsALet.
3,772.95
2.22
You cannot declare that again.
3,775.17
2.64
And so const and let actually protect you
3,777.81
1.71
from declaring something with the same variable name twice, which is something
3,779.52
4.8
that var does not do.
3,784.32
0.97
Cool.
3,804.37
0.69
What do you guys think would happen if I tried to do this?
3,805.06
2.416
Anybody care to guess?
3,817.83
1.03
Undefined?
3,821.85
1.66
So let's try to run it.
3,823.51
2.8
Error.
3,826.31
1.03
So since these things are block scoped, it
3,827.34
2.55
means the variable is declared at the line that it is written.
3,829.89
4.59
And if we try to use it before then, it actually does not even exist at all.
3,834.48
3.64
So if I tried to console.log something called thisIsAConst here, remember,
3,838.12
3.14
the JavaScript interpreter is just reading down
3,841.26
3.24
and it won't see like, hey, what the heck is thisIsAConst?
3,844.5
2.42
I have no idea what that is so I'm just going to Error here.
3,846.92
2.5
Same thing if I tried to do this as a let here.
3,852.66
3.6
That will also Error with the same error.
3,856.26
2.246
AUDIENCE: So what's line 14 error on?
3,858.506
4.444
JORDAN: Why does it error?
3,862.95
2.16
Because we're trying to declare two variables with the same variable
3,865.11
4.59
name twice.
3,869.7
1.02
So we have let thisIsALet equal 51 here, and we try to do it again
3,870.72
3.6
and it's saying, hey, you already have a variable called thisIsALet,
3,874.32
3.06
so I'm not going to allow you to do that again.
3,877.38
2.5
So it's going to be this error right here.
3,879.88
3.398
AUDIENCE: So it's the instantiation of it?
3,883.278
2.891
JORDAN: Yeah.
3,886.169
0.541
It's saying like-- basically I'm saying, hey, give me a variable to call,
3,886.71
3.219
this is a let here, and then a couple lines later I'm saying,
3,889.929
2.541
hey, give me a variable called thisIsALet,
3,892.47
1.77
and JavaScript says, hey, like, you already
3,894.24
2.19
have a block scope variable called thisIsALet,
3,896.43
4.23
so I can't give you another one.
3,900.66
3.359
However, if I tried to just update it like this, that's totally OK.
3,904.019
2.791
Any other questions here?
3,909.489
1.041
So the other thing we said, we can have these things called a var.
3,913.79
3.27
If I did var thisIsAVar equal to 50, that's fine, I can update it.
3,917.06
8.01
That's also fine.
3,929.21
0.87
I can also do this, and that won't yell at me.
3,930.08
8.35
So vars are older ways to declare variables
3,938.43
4.437
and they don't have the same protection that let and const do.
3,942.867
2.583
You can override them like this and it's totally OK.
3,945.45
4.532
And you can actually declare a whole new variable with the same variable name
3,949.982
3.208
and that's also OK.
3,953.19
2.67
And something-- yeah?
3,955.86
0.991
It just-- yeah, so that's--
3,960.08
3.11
So if I try to run this, it will update the old one.
3,983.27
4.02
It'll just replace that value.
3,987.29
1.25
It's actually called shadowing, where you create a new variable
3,988.54
2.71
with the same variable name which basically just overshadows
3,991.25
2.88
that old one.
3,994.13
0.66
So it's as if the other one didn't exist.
3,994.79
1.77
Cool, so check this out.
4,002.63
1.17
So this errors and we know this errors.
4,008.22
2.87
But say I were to do this.
4,011.09
3.73
You'd expect this to also error, but actually it does not.
4,014.82
6.11
It returns undefined.
4,020.93
2.361
So this is another weird thing about JavaScript.
4,023.291
1.999
This is called hoisting.
4,025.29
2.129
And so certain things are hoisted, which it
4,027.419
1.791
means basically it takes the definition of something
4,029.21
3.63
and hoists it to the very top of the file and does that first.
4,032.84
3.06
Get
4,039.07
0.742
And a few things are hoisted.
4,039.812
1.648
Var the-- actually, the declaration of the creation of an empty variable
4,041.46
5.04
are hoisted.
4,046.5
1.38
Function definitions are hoisted as well, but const and let
4,047.88
3.28
are not, as we saw if we tried to access the variable name of the const or let,
4,051.16
4.01
then it errors.
4,055.17
0.86
But with a var, the declaration of that variable is actually hoisted.
4,056.03
5.289
And we can talk a little bit more about how that works in a second,
4,061.319
2.791
but let's actually play with it a little bit more.
4,064.11
2.23
So function definitions are hoisted.
4,066.34
2.33
So let's clean up this file a little bit.
4,068.67
3.92
So let's call this new function, called--
4,079.332
1.708
so this is console.log.
4,087.262
1.768
So I defined this function called thisIsHoisted
4,104.302
1.958
at the very bottom of the file.
4,106.26
1.5
And all it does is it console.logs, there's
4,107.76
2.4
a function declared at the bottom of the file.
4,110.16
2.01
But something very interesting is at the very top of this file, I can call it.
4,112.17
3.689
And that will actually work.
4,120.884
1.166
And so that is what's called function hoisting.
4,125.229
4.88
The behavior whereby a function definition
4,130.109
3.221
declared at the very bottom of a file is actually available for use
4,133.33
3.84
at the very top of the file as well.
4,137.17
1.5
But this does not work in other cases.
4,141.34
2.523
So say I were to do something like this.
4,143.863
1.666
So who can tell me what the difference is between line 21 and line 25?
4,166.26
4.485
They look pretty similar, right?
4,173.42
1.559
Yeah?
4,174.979
0.499
OK.
4,180.62
0.5