- [Instructor] All right so let's explore the
block scope variables a little bit further in this video.
So let's remove all this code and let's start from scratch.
The first thing I'm gonna do is
declare a let variable,
so let's do a warrior one
and let's assign again the ninja
and let's do a const right after that
and let's do a warrior two.
Let's call this guy viking.
So if I console log these two right now
and because they are in my scope
I shouldn't have any issues, so let's start with that.
So let's save, let's go back here,
and we have ninja and viking, so we're good.
Then if we want, and I'm going to
be proactive and this will return
an error.
If we were to reassign a warrior two,
so let's do warrior two
and reassign it to, let's say samurai,
and then console log
warrior two
we'll have an issue,
because this is a variable that can not be mutated.
So let's go here and we got
assignment to constant variable error.
So we can't do this.
This is a const, but if we did this to
let's say warrior one
and then console logged warrior one again
because it's a mutable variable
we have no issue here.
So let's save this, let's go back here, and we are good.
So let me give you another example.
So let's create
a function called screen warrior,
the same function we've been using since the beginning
and then inside let's do an if statement.
So let's do if
warrior two,
I think there's a little typo here, equals
viking
then return this or do whatever is in there.
So what we're gonna do is also create a new variable.
We're gonna do a let warrior three
equals
infantry solider.
Let's do a console log
and let's do template strings,
our army or consists of
warrior one.
Let's copy and paste that little
portion of the code comma paste
and then do two
and then let's do the same and warrior three
exclamation mark.
Okay,
so now if warrior two
is a viking, so that will be met
with a true then it is going to create
this variable that is block scope again,
because we're using a let and we're doing this
inside of the if statement here.
It's not going to be available either here or outside,
because it's block scope.
So this is only available within
that block here.
I can show you
by doing a console log right after of
and we'll comment that for now,
because we want to make sure that our function works first
but then what you could do is do a warrior three
and this is not going to be available
either here or if we copy and paste that outside
there either
once we run our function.
So let's go and
run the function
and save and let me comment that
for now, because I want to show you while it works
and then we'll show you that this variable here
that is blocks scope, so only available
inside of that block
cannot be used here or there.
So let's first start with this
and save
and let's go back to our browser.
Now our army consists of samurai, viking,
and infantry solider.
So as you can see in here we have access to warrior one,
warrior two, and warrior three,
which has been declared inside of the block,
but if we were to comment out the console log here
and save and go back here
we would have an issue because warrior three
is not defined or not accessible there.
So let's comment this line and then let's comment out
in here in the global scope,
save that and we get the same result.
So as you can see when we use let or const
it's only available within the block scope it is part of.
So right now these two are available across here,
because they are declared at the top level.
This guy here is only available within the if statement,
because it is declared here.
If I was to declare something like
let warrior
four
equals,
let's call this guy just
cannon shooter and here
use warrior four,
save that,
and let's comment this guy here so we don't get any errors.
Let's go back to our browser
then we have access to this guy
because it is within that block scope here,
so it's within the scope
of the function screen warrior
and it's used inside there.
If I use warrior four here we'd have an issue.
So let's save and then go back here
and we have a reference error.
So this is how block scope works.
So when you actually use let and const
make sure that you're using it
or referencing it inside of the same block.
Let's move on.