Let vs. Var vs. Const
Let’s talk scope in JavaScript. New to ES6 (or ECMAScript2015 or whatever it’s being called these days) are the variable declaratives let and const. We can still use var, which we’ve always used, but let and const give us some different scoping options.
So what are the differences?
1. Var can be redefined or updated. You can redeclare a new var with the same name and JavaScript won’t have any issue with that.
2. Var variables are function scoped. This means that the variables are only available within the function in which they are called.
3. If called outside of a function, var variables are globally scoped, which means they are available in the entire window. Generally, you want to keep your variable declarations inside of functions.
4. Let and Const are block scoped, which means they are available through the entire block of code in which they are called. In JavaScript, anytime you see curly braces, { <- these are curly braces -> }, that’s a block.
Functions are also blocks, let and const are still going to be scoped to a function, but let and const will be scoped to the closest set of curly braces.
5. Let and Const can only be declared once.
So, are Let and Const the same then?
Nope. Here are some ways they are different:
1. While let and const can both only be declared once, let values can be updated. Const variables cannot be updated.
2. Properties of a const variables can be changed, however, the entire variable cannot be reassigned. For example:
In the above code, I may want to update some of the properties, but I would never reassign the entire variable.
If you DO want to make everything unchangeable, we have the option to freeze it.
This isn’t new in ES6, but it is useful to know about.
How much deeper can we go?
Pretty deep, but that’s for another blog post.















