Variables

In earlier versions of JavaScript, variables were solely declared using the var keyword followed by the name of the variable and a semicolon. This is how we would do it.

var variableName = '';

After ES6 (a newer version of JavaScript) we now have two new ways to declare a variable: let ****and ****const.

We can take a look at both of them one by one. The variable type let shares lots of similarities with var but unlike var it has some scope constraints. The scope is out of "scope" of this introductory video but we will explain it in great detail in a later video! The only thing that you need to know right now is that let is the preferred way of creating variables in modern JavaScript.

let variableName = '';

Const is another variable type assigned to data whose value cannot and will not change throught the script.

const variableName = '';

The variableName is the name of the variable which you can freely choose.

What do you think, can we name our variables literally anything?

We have a few criteria when it comes to creating variable names, also known as identifiers.

We have a few rules when it comes to creating an identifier in JavaScript:

  • the name of the identifier must be unique
  • the name of the identifier should not be any reserved JavaScript keyword (for example, we cannot declare a variable like this: var let = 0;
  • the first character must be a letter, an underscore (_), or a dollar sign ($). Subsequent characters may be any letter or digit or an underscore or dollar sign.

To recap, there are three different ways to make (or declare) a variable: var, let and const.

From now on, whenever we're creating a new variable, we're going to use either the const or the let keyword. const when variable is going to be constant and let when we plan on changing it!

Let's move on to data types to see what kind of data can we store inside of variables!

Complete and Continue