Last updated on July 13th, 2020 at 02:40 pm.
So as we said, a variable stores some value in memory.
If I come here and I try to use the ‘let’ keyword to redeclare this, the ‘let’ does not allow us to redeclare the same variable identifier.
So this is an identifier.
So if I say ‘let’, this would be like redeclaring this because the value is already stored in the browser’s memory.
So if I come here and I run this, you’ll see it gives us an error.
It cannot, it cannot redeclare ‘a’.
So if you want to try an example with the ‘let’, we have to use a different identifier.
Let’s use ‘b’.
And then if you come here and say ‘b’, then we run this, ok, ok, I think I used ‘b’ at some point.
And all of this is now being stored in the browser memory.
You can see what a variable actually is.
It just stores these things in memory until you change its value.
So let me use ‘c’, because I’ve used ‘b’ and I have also used ‘a’.
So ‘c’, ‘c’ is not in the memory.
If I come and I run, you’ll see in this case it’ll run just perfectly fine.
So this is a very good example for what a variable really is.
And since the ‘let’ keyword which is different from the ‘var’ keyword does not allow us to change the value of an identifier within the same scope.
So we’ll look at scopes later own.
Within the same scope, you cannot change the value of a variable that you have declared with the ‘let’ keyword.
And you’ve just seen a perfect example for that.
So that’s one reason why it would be better to use the ‘let’ keyword.
So as we saw earlier, we can also declare a variable without any keyword.
It’ll still work.
So I can say alert(c) and if I run this, you see it gives us this one.
If I change the value just to confirm and then run it, you see it gives us a different value.
So you can see that declaring a variable with the ‘var’ keyword gives you a variable that can be changed at any time.
It can be mutated.
A variable declared with the ‘var’ keyword is mutable within that scope.
If you use the ‘let’ keyword, it’ll give you a variable that cannot, you cannot redeclare it using the same name.
You can declare var x=10; then var x=20; But you cannot say let x=10; and then again let x=20; If you declare with this, this is just the same as this, except this one is a loose variable.
This is a variable that is accessible from outside the scope of the code that it is running.
So in the next video, I want us to look at something to do with ‘undefined’ and ‘reference’, and something to do with ‘reference error’.
So I’ll see you in the next video.