Last updated on July 13th, 2020 at 02:33 pm.
Hey, and welcome back.
So in this video, you want to look at an example.
An example that shows us something about Variable Scoping.
So if I come here and I open Scratchpad, and on Windows you can do that by Shift+F4.
So I’ll just come here and copy this code.
I want to use this for our example.
And now, you know in our examples we’ve been using ‘alert’ and not console log.
So in this case, this is a local variable within this ‘if’ block.
And yet, we can still access it from outside here.
So that is when we use the ‘var’ keyword.
It’s very important to note this.
If you use the ‘var’ keyword, this variable is accessible from outside of this block.
So if I run this, you’ll see it gives us 5.
But then, you know that we also have without, without the ‘var’ keyword which is not advisable to be used at any point.
So even with this, if I run this, it will still give us 5.
And now, we also have the ‘let’and with the ‘let’, if you declare a variable using ‘let’, this will not be accessible from outside of the block.
So that’s one reason to use the ‘let’ keyword.
So if you use ‘let’, it will not be accessible from outside.
And since we have used ‘x’ as a variable, we cannot use it for the ‘let’ again.
And we saw this in previous videos.
So I will change that to ‘y’ and that will also become ‘y’.
Then, we run this and you will see the reference error.
This is something that we spoke about.
And we said that, if you see the reference error, this basically means that the variable has not been declared.
So if you use the ‘let’, you can see this is the true, this is a true local variable.
If you use the ‘let’ to declare a variable.
So we also have ‘const’.
So a ‘const’ is also new in ECMA2015 and if we use ‘const’, ‘const’ which simply just means that this variable to some extent is immutable.
But it is not a constant like in other programming languages.
It is not a constant with the same principles and with the same nature of behavior.
But it is not exactly the same as a constant in other programming languages.
So for this one also I will not use ‘y’.
It will bring issues.
But I can use ‘z’.
So if I use this ‘z’ and I try to run this, you’ll see it also gives us a reference error.
So you can use the ‘const’ for something that you know is not going to be mutable.
So in this video, we’ve seen examples of variable scoping.
So, as we saw in other videos, you saw that if we take this and you put it up here, there are cases if you use the ‘var’ keyword, it’ll show you ‘undefined’.
So, you can do an example with those and see how it is.
Just take the variable and put it up here for the ‘let’, for the ‘var’ keyword.
If you use the ‘var’ keyword, you can try and put this up here and see what will happen.
Think of that as your assignment and as a way for you to practice this for yourself.
So I will see you in the next video.
And in the next video, we are going to look at Variable Hoisting.
Recommended VPS Providers
Find out the VPS providers I recommend.