>> [MUSIC] Hi. My name is Aaron Powell.
I'm a Principal Cloud Advocate here at Microsoft.
In today's video, we'll have a look at a couple of demos of how
we can use the different kinds of variables in JavaScript.
Let's start off by declaring some variables,
and we're going to use the different keywords.
We'll start with the var keyword for declaring a variable.
I'm just going to create a variable, we'll call it hello,
and we'll assign it Hello" as a string.
Then if we do console.log
and then provide it with the name of the variable,
I hit "Save" on that file and then run this on
a console node demo.js.
We will get as we expect
the variable to be output and it comes out with the string hello.
Now, something we need to be aware of with the var declaration for
variables is that this is a function scoped declaration.
That's important because it
changes how that variable is available.
Now, while it might not be clear for
this file that we have a function scope here,
we have a function scope inside of this file that's
just implicitly put there by Alt-Node.js runtime.
We can think of this file as
one giant function that's being executed,
and that's the scope in which our var variable is defined.
Now, with the var variable, because it's
defined using a function scope,
it's always available within inside of that scope.
I've just moved the declaration down to line 3 instead of line 1,
and if we were to now run this from our console,
we would get the output of undefined,
which makes sense if you understand that this is a function
scoped variable because the variable is
actually defined within inside of this whole function scope,
not just from the point that you've used
the var keyword to declare it and fill it on.
Our variable is available
on line 1 when we did our console.log statement.
We just haven't assigned it a value,
so it's implicitly undefined.
Now, this is different to when we have block scoped variables
such as let and const. Let's have a look at let.
Let is a block scoped variable that works similarly
to the way that we can work with
var in that we can actually reassign it.
Actually, let me just move that line
down and show you what I mean by reassignment.
I've output initially with hello and now if I
change our hello variable to be hello world,
and then we were to do our console.log statement,
if I can spell console correctly.
Now, if we were to execute our code again,
we would see that this does output the two as we expect,
hello and hello world.
Now, we've reassigned this variable,
and we can do that with let but let is going to be block scoped.
Let's create a block and we'll just do an if true statement.
This is just going to always be an
executed if condition because it's always going to result in true.
Instead of here, we'll do let world equals hello world.
Now, if we do console.log of world, save our file,
and then rerun, we should end up with
the second hello world output as we were expecting
because this variable has been defined and it's available for use.
Let's then move that upper line.
Now, we're attempting to use it before we have declared it.
We'll save our file,
execute for the command line.
This time, we've received a runtime error.
The reference error cannot access world before initialization.
That's because we've tried to use the variable
before we've initialized and declared that variable.
This is something that will happen at runtime if you try and use
a let keyword to create a variable,
and then use that variable before you've actually defined it.
Also, because this is only
available within inside of the block scope,
if I was to move the console.log
statement to after the closing curly bracket,
which is where our block ends,
and execute the code again we will get a different runtime error.
Now, world is not defined.
That's because it's now go within inside of the scope.
We've moved out of the scope where
that variable was initially declared,
the block scope, we
can't access that declaration of that variable.
This is similar to how the const keyword will work.
Const works like let,
not world, that's never our variable.
Const, I will create it and we'll just give it a name,
we'll call it Aaron and we'll give it my name.
Now, if I was to do console.log(aaron),
we will get as we expect the output in our console,
so we'll get that variable output.
Being a block scoped variable,
if I was to put this inside of a block
like I did with our let bound variable,
then it would be only available within inside of that.
But that's the thing that is in common between let and const.
But the other thing that let has going
for that const doesn't have is the ability to change.
If we were to try and change the variable,
so let's try and change Aaron to Aaron Powell,
so my full name, and then run our code again.
Now we're going to get a different runtime error.
Sorry, that's obscured a little bit.
Let's just bring it up again.
Run it so you can see it from behind my head.
Now, we've received an error saying that
the assignment to a constant variables happened,
and that's the type error.
This is because we've attempted to assign a const.
We can't assign a const because it's a constant.
It's the value that was originally assigned to it,
it's the value that will always be assigned to it.
Now, if you want to use a variable declaration,
you've got the choice between const, let, and var.
Const is the one that I would recommend you use most
frequently because this helps you as
the reader of your own code and other people reading your code
understand that the variable you've
defined should only be used once,
and that's the time that you want to
be using it and then you can't reassign it.
Our let bound variable will be useful though still inside
of specific blocks that are being reused,
such as a loop block like a
for loop or a while loop and things like that.
That time you might want to use a let bound variable instead of
a const bound variable because you need to be able
to change the value for each iteration of the loop.
There's very few reasons why you would need to use the var keyword
though because as a function to a variable,
its availability is increased exponentially but also it has
some challenges around when
that variable is available as we saw initially.
We can accidentally use it before we've even defined the variable,
which should be quite confusing
when you come back and look at it later.
Thanks for watching and we'll see you next time.