With ES2015, a slew of new features were released (ES6). And now since it’s 2020, it’s safe to presume that many JavaScript developers are familiar with and utilizing these functionalities.
While this assumption may be somewhat correct, some of these features may still be unknown to some developers.
The addition of let and const, which can be used for variable declaration, is one of the new features in ES6. What sets them apart from good ol’ var, which we’ve been using, is the question. This post is for you if you are still unsure about this.
In this post, we’ll look at the scope, use, and hoisting of the variables var, let, and const. Take note of the contrasts between them that I’ll point out as you read.
So, in case you missed it, here are the differences:
- Let and const declarations are block scoped, while var declarations are globally or function scoped.
- Within the scope of the variable, var variables can be changed and re-declared; let variables can be updated but not re-declared; and const variables cannot be updated or re-declared.
- They’ve all been lifted to the apex of their abilities. However, whereas undefined is used to initialize var variables, let and const variables are not.
- Const must be initialized upon declaration, but var and let can be declared without being initialized.
Source: https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/