Of course, there is nothing to prevent you from starting from here, but I think that understanding the previous parts is much more important than reading about the new features in ES6. However, if you would like to start right away from this place, nothing stands in the way
Topics that I will touch are:
- let / const
- arrow functions
- spread operator/rest parameter
- array helper methods
Ok, for starters, what ES6 is?
We will start with something very simple but also very useful. Let and Const.
We all know what the var keyword is. We have used it many times. New variable? var, new Function Expression? var etc. ES6 introduces two additional keywords, which are let and const. This does not mean that var will stop working now, but you are very encouraged to use the new ones.
Short, It is really a new var. Let is encouraged to be used everywhere instead of the mentioned var. By everywhere I mean anywhere except when we want to use const (explanation is few lines below). Use let for example if you want to create a simple variable.
var myVar = 100; let myLet = 100; console.log(myVar); console.log(myLet);
Let's follow another example. Here we can see the function which contains two for loops. The only difference is exactly the var and the let. However, if we want to check the values of I and j in the console, outside the block where the code is executed, we will get two different results.
When var i = 0, our i variable will be visible.
However, if we use let j = 0, our j variable will not be visible
The main reason we want to use the const keyword is when we don't want a variable to be re-declared in later code. For example, I strongly recommend using const as an old var and change to let when it is needed. Let's see what will happen when we decide to change the variable that we declared using a const keyword.
const myArray = ["Do", "not", "change", "me"]; myArray = ["why?"];
Of course, it is still possible to use methods such as push, etc, but as shown above, any "manual" change of the previously declared value will end with an error.
const myArray = ["Do", "not", "change", "me"]; myArray.push("ok"); console.log(myArray);
Our array has been modified.
In the next article, we will discuss another topic from the list: Arrow Functions.