ES6 Lesson #2 - Let and Const

In the es5 jаvascript version, there is only one variable declaration method - var. And this method declares the variable globally across the entire document. Sometimes this confuses developers, especially those who switched to working after C or java.

There are more logical variable declarations in ES6 - Let & Const.

Let

The let declaration only allows the use variable within the current parentheses. For example:

if(true) {
  let valera = "Im working";
  console.log(valera) // return Im working
}

console.log(valera) // return declaration error.

If you will trying to do the same with var, then the valera variable will work in both cases. Let can help you a lot if you have a large document and there are many functions in each of which there are similar variables. Such code is better because it gives less load on the browser, as it does not declare variables that are not used. 

Const

Const is not a variable, since its value cannot be changed. Const can declarated only one time on page. It is necessary for variables in which the values do not change, since it gives a low load on the browser. As with let, const only works within quotation marks in which it is declared.

const AL = 115;

if(true) {
    const VL = 10.245;
    console.log(VL) // returns 10.245
    console.log(AL) // returns 115

    VL = 45 // returns error
}

console.log(VL) // returns error