ES6 or ESCMAScript 6 or the European Computer Manufacturer’s Association Script 6 if your really want to be the most verbose, is the newest standardized version of JavaScript as of this date. Although ES6 was released in June of 2015, still see many programmers not using ignoring some its useful features. =So, let us talk about a one of ES6’s most valuable feature const and let and when to use them (hint always use them over var).
Const and let, are two new ways ES6 allows us to declare variables, gone are the days of var and most experts agree to almost never use var again. Var was in my opinion a little too unspecific in its duties, especially as a developer who mostly used Java and C in his schooling. Var was the only way to declare a variable, it also had the ability for whatever reason to be redeclared at the same scope or deeper into that scope allowing you to hoist the new declared version of the var variable out of the deeper scope. As to why you would do this? I am not sure, maybe there is a scenario out there but seems like it could only cause problems. That’s where the two new ways of declaring variables come in.
Let
Let’s talk about let, which both of these declaration are pretty self-explanatory but I love the idea of let as it kind of infers its meaning as “lets say this variable is i = 0” this variable similar to var can be mutated but cannot be redeclared inside its own scope here’s an example of the difference.
let msg = ‘hello world’
let msg = ‘what’s up world’
if(msg.length > 4) {
let msg = ‘one level deeper into the scope’
console.log(msg)
}
Console.log(msg)
This piece of code will throw the error:
error: unknown: Identifier 'msg' has already been declared.
But if we take out the second declaration but keep the third like this:
let msg = ‘hello world’
msg = ‘what’s up world’
if(msg.length > 4) {
let msg = ‘one level deeper into the scope’
console.log(msg)
}
Console.log(msg)
Console:
“one level deeper into the scope”
“what’s up world”
Meaning if the variable can be “re-declared deeper in the scope” but you are actual just creating a new variable deeper in the scope that happens to have the same name which is not a great practice as you will not be able to use the one declared before that but you can still do this if you’d like. The important thing is that you cannot technically re-declare the variable which is very important to keep yourself sane as a programmer. Var on the other hand you can do whatever you want for example:
var msg = ‘hello world’
var msg = ‘why would you want to do this?’
console.log(msg)
if(msg.length > 4) {
var msg = ‘why can you do this it’s so dumb!’
}
console.log(msg)
console:
“why would you want to do this?”
“why can you do this it’s so dumb!”
As you can see for whatever reason you can basically do whatever you want with var regardless to how unsafe and unpractical.
Const
Now lets talk about const, const is even more straight forward than let simple put const stands for constant mean once you use const to declare something it cannot be declared again like let and it cannot be reassigned to a different value at that scope level. So, for example let:
const msg = ‘hello world’
if(msg.length > 4) {
const msg = ‘one level deeper into the scope’
console.log(msg)
}
msg = ‘wait I want the message to be something else'
console.log(msg)
console:
error: Uncaught TypeError: Assignment to constant variable.
This code through the error because you cannot change the value of a const but if we take that line out:
const msg = ‘hello world’
if(msg.length > 4) {
const msg = ‘one level deeper into the scope’
console.log(msg)
}
console.log(msg)
console:
“one level deeper into the scope”
“hello world”
As you can see much like let it can be redeclared once your scope is a level deeper but will ”revert” back to its original value once out of that scope. Meaning the values if “redeclared” stay with the scope as a new variable but has nothing to do with the first variable in a sense.
So, overall stop using var it is never a good choice let is the right choice for any variable you are changing like a sum, temp, or count. Const is a great choice for well a constant variable a global, something like a URL that will not change. I hope this overview of const and let was helpful and helps you with future projects.