- Learn the two new variable declarations introduce with ES6
- Understand the differences between let and const
Names equal an array with a single element with the value of James. Then I want to execute this. And in the console, we can see an error, assignment to constant variable. As I mentioned earlier, this is a read-only reference, but it doesn't mean that the data is immutable. I'm gonna go ahead and change this to names.push passing through a value of James, and I will execute this. And in the console, we can see that James has been added to the end of the array. This works because the variable is still referencing the same initial array that currently exists in the system's memory.
Now, ES6 does have a variable declaration that does allow for reassignment. That is called let. So I'm gonna go ahead and replace const with let. And now on the following line after the console log, I'm gonna reassign the names variable value to a new array with a single element that has a value of Lisa. I'm gonna copy and paste the console log on to the following line. And now when I execute this, in the console we can see two messages. The value of names after I pushed the value of James to the array and the current value of the names array, which equals a single element with the value of Lisa. So, if you want to declare a variable that forbids reassignment, use const. Otherwise, use let.
Another difference between ES6 variables and var is you're not allowed to re-declare a variable that already exists. To demonstrate this, I'm gonna do some code cleanup. And now on the screen, there is the names array and a console log of the names array. Now, on the fine line, I want to re-declare the names variable, let names equal array with a single element value of Lisa. And now I'm gonna execute this in the console, and the error has occurred. Identifier names has already been declared. Now, this has to do with how let and const are scoped. They are both block scope while var is functional scope. Any discussion into scoping is out of scope for this video, pun intended.
So when should we use ES6 declarations versus using var? Well, if you're working with the older code base, you will most likely not be using ES6. In the end, it comes down to your work policy. And with that, I'll end this video. Thanks for watching at Cloud Academy.
Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.
Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.
Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.
Farish is an avid powerlifter, sushi lover, and occasional Funko collector.