
Let’s see what happens when we change the value of the property in an Object. Above example is only for Primitive variables. See, when we try to change the value, it throws a TypeError. It is same as let keyword, but the difference is that it does not allow you to change the value once it is declared. Since the variable education is already declared, it throws error as above. Let variables cannot be redeclared in the same scope. Now, see it throws error when we access the age variable before declaration. It solves the hoisting issue we can see how it is.

You can see that the variable name is not overridden, since we declared the name as a block scoped variable so the variable inside the if block doesn’t have access to the variable outside the If block. Let is used to declare the variable in block scope and it doesn’t allow us to use the variable before declaration.

It allows us to redeclare the same variable name in the same scope, but it takes the most recent value. That’s why it prints undefined on the console. Variable age is hoisted at the top of the scope but note that variable is only hoisted not the value ‘ 20’.

But wait!! Why it prints undefined in the console even though It didn’t throw any error? You can see that it didn’t threw any error when we access the age variable before declaration.

Seems no issues right!! But the problem is when we need to use the same variable name in multiple places, it forces us to use different variable name every time and it leads to unnecessary memory allocation.Īnother problem is hoisting, let’s see what it is, So, we got the output ‘Warner’ on the console. We expect that ‘ James’ will be printed on the console, but since the variable name is global scoped, the variable name outside the if block has been overridden by the variable inside the if block. Let us discuss some points about Var keyword here: Through this article, we have given more importance to example codes. You could have surfed over the internet to gain the knowledge of ES6 topics here and there depending on your development needs, but here you are going to gain the most useful skills of JavaScript ES6 features in one place.
