In the first example we see that wen we set var in the global scope we can reassign and change the value of the variable, but in the second example we can see that we can not access the variable because when we go out of the function we are out of its scope.
While a var variable can be redeclared and updated a let can be updated but not redeclared for example:
As we see above when we try to redeclare the let variable within the same scope we get an error that it has already been previously declared. But what let does allow us to do is update the value of the let variable as we see in the second example. Depending on the scope of the variable we can do what seems like reassigning the variable. As we see in the example below, the isTall variable might seem like they are the same variable, but in all actuality since they are in different scopes they are two different variables and when we call isTall outside of the the function we will get false because we are pointing to the isTall variable that was declared outside of the if statement.
The major difference between let and const is that const is a fixed variable which means it can not be updated. For example:
One cool thing with const is that if we set a const variable to an object the attributes of that object can still be changed for example:
if we didn’t want the attributes of the above object to be allowed to be changed we could use a .freeze, but thats a topic for another blog post.
As long as you are aware of what you are doing all three of these variable declarations are good to use and have a purpose in the grand scheme of your program. Although you will mostly use let and const there are reasons and opportunities to use var instead. Thank you for reading and hope this helped.