Javascript ES6 — const vs let # 2

As a reminder from the previous video, you already know that the keyword var
is supposed to create global variables that can be accessed from the global
scope, and you learn also that the var keyword is function scoped.

Now we’ve to deal with two new keywords, let and const which are block scoped,
that means that if you create any variable within a block, as an example a
function, a condition, or a for loop, it will be accessible only within its
scope, still there are some differences between let and const that we’ll
discover on this video.

let

You’ve some rules to understand when you decide to choose a specific keyword
to create a variable, lets first create a new variable using the keyword let
let number = "367”, the question is, what kind of rules are
applicable on the variable when you use the let keyword ? from this example,
you already now that this variable number is created on the global scope, and
the let keyword is a block-scoped variable.

The first applicable rule for the let keyword is, you can’t
recreate the same variable within the same scope,if I create the same variable
number let number = 786 even with different value 342
let number = 342 it’ll throw an error
Identifier 'number' has already been declared. However this rule
will not be valid if you create another scope within the global scope, as an
example a condition if (true) { let number = 123 }, in this case,
you’ll have two variables number each one is stored on its specific scope.

Imagine that as a box within a box, when each box has his own elements. Doing
a console.log from each scope will throw different numbers,
console.log(number) after the first variable display 786, and
console.log(number) inside the condition display 342. To
summarise you can’t recreate the same variable unless you’re within a child
scope.

The second rule is that you’re allowed to update the value of a variable using
let within a scope, let’s update the number variable within the global scope
number = 564 and the condition scope number = 198.

The last rule is that you can’t access to a variable using let outside of its
scope, lets remove the number variable created on the global scope, and move
the console.log(number) outside of the condition, now I have an error
number is not defined, so duplicate the console.log(number) and
move it into the condition, so I can access its value within condition but not
outside.

const

Lets move to the const keyword, the rules are similar to the let keyword, and
the only difference that a constant can’t be updated, let’s have a simple
example of const const API_KEY = "hello098”, as you see I name
the constant using uppercase letter, you’re not obligated to do so, this is
just a naming convention for naming constant.

By using the keyword const to create a variable you’re telling your program
that this is a constant, and it has not to be updated once is created, so if I
try to update the value of API_KEY to 123 API_KEY = 123 I’ll get
an error of Assignment to constant variable that means that I
can’t update the value of API_KEY once is already created, using const can be
useful if you want to disallow the logic of your program play with its value,
lets take an example of updating a const through a for loop, before that lets
change the value of const API_KEY to the number 768.

And lets create a for loop, initialize the index i to
0 and I want this loop to do 3 steps so i is less than 3
i < 3 then I increment the index by one, open the bracket, and
I’ll try to increment the value of API_KEY by one API_KEY += 1 on
each step, same error telling me that I can’t update the value of API_KEY.

To recap

  • Avoid using var in general
  • Always use const to create a constant or a reference
  • Use let for everything else

Leave a Reply

Your email address will not be published. Required fields are marked *