1. Home
  2. Training Library
  3. Programming
  4. Courses
  5. Introduction to JavaScript ES6 Variables

Introduction to JavaScript ES6 Variables

Contents

keyboard_tab
JavaScript ES6 Variables

The course is part of this learning path

Introduction to JavaScript ES6 Variables
Overview
Difficulty
Intermediate
Duration
4m
Students
25
Ratings
5/5
starstarstarstarstar
Description

This practical course explores JavaScript ES6 Variables. You will learn what they are and how to use them.

Learning Objectives

  • Learn the two new variable declarations introduce with ES6
  • Understand the differences between let and const

Intended Audience

This course is intended for anyone who wants to learn about JavaScript ES6 variables.

Prerequisites

Anyone with an interest in JavaScript ES6 declarations or who wants to improve their knowledge of JavaScript in general.

Transcript

Introduction to JavaScript ES6 Variables. With the release of ECMA Script 2015, aka ES6, JavaScript introduced two new variable declarations, const and let. And this video is a quick primer on these two new declarations. Currently, I have a const declaration called names that is equal to array of names. When using a const declaration, you are creating a variable that is a read-only reference to its name. This is not to be confused with immutable variables. To demonstrate this, on the next line, I'm gonna reassign this variable a new value.

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.

About the Author
Avatar
Farish Kashefinejad
Full-Stack Development Content Creator
Students
232
Labs
6
Courses
7
Learning Paths
1

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.

Covered Topics