by Mariya Diminsky
Learn ES6 The Dope Way Part I: const, let & var
Welcome to Part I of Learn ES6 The Dope Way, a series created to help you easily understand ES6 (ECMAScript 6)!
First up, what’s the deal with const, let, and var?
You’ve probably been a witness to one of these situations — let and/or const being substituted for var, or let and const being used in the same code at the same time, or even more perplexing, let, const AND var all being used at the once!?
Hey no worries, I got you. Let’s clear this fog together:
- Useful if you’re setting a variable that you don’t plan on changing.
- Protects and prevents your variables from reassignment.
- In compiled languages, there is an increase in runtime efficiency of your code and thus an overall performance boost vs using plain ‘ol var.
- Works as it should in Chrome and Firefox. But not in Safari. Instead it acts as a normal variable, as if it were var, and thus can be reassigned.
- Generally there is programming convention to set the name in all caps to show others reading your code that the value of the const value should not be changed — you will witness both lowercase and caps const coding situations. Just something to be aware of.
Does that make sense?
Think of const, like the constant sea — never-ending, never-changing…
…except in Safari.
Students and experienced programmers coming from a Ruby or Python background will love let, as it enforces block scoping!
As you migrate over to ES6 country, you may notice yourself adjusting to a new let metamorphosis taking over your coding style, and find yourself less likely to using var anymore. With let it’s so much more clear now where your values are coming from without worrying about them being hoisted!
- Block-Scoping, your variable’s values are exactly as they should be in that current scope and they are not hoisted as with var.
- Super useful if you don’t want your values to be overwritten, like in a for loop.
- You may not always want to use let. For example in situations where variables aren’t as easily block scoped, var may be more convenient.
Do you see the difference? It’s all about scope. With var, it has access to it’s parent/outer scope and thus can change the value inside the if statement. Unlike let which is block-scoped and can only be altered within the current scope it’s in.
let is super straight-forward. It’s like a person who speaks straight to your face and tells you exactly what they need right then and there while var does this as well but may occasionally reply with unexpected answers — due to hoisting and access to outer scope variables. Depending on the situation either one may be in your favor.
Another great example on the benefits of let:
Say you want to create a game board of 30 divs and each one has their own value. If you were to do this with var, the i index would be overwritten for every iteration — every single div would have the value of 30! Yikes!
On the other hand, with let, every div has its own value, as its own div scope is maintained for each iteration! See the difference:
Congrats! You’ve made it through Learn ES6 The Dope Way Part I and now you know the main differences between const, let and var! Woohoo! You rockstar, you :)
Keep your knowledge updated by liking and following as more Learn ES6 The Dope Way is coming soon to Medium!
You can also find me on github ❤ https://github.com/Mashadim