Advantages of let

The block scope where let "lives" help us to minimize the chances to make errors. 

Imagine that you have a large piece of code and you use var to declare your variables.

There is a chance, that by mistake you use the same variable name twice without realizing it.

For example:

function myFunc () {
var x = 100;
var x = 200;
console.log(x);
}
myFunc ();

In this case, the output of the console will be 200. Here the code is too small and you can easily control what you are doing. However, if we had 1000 lines of code, you wouldn't be able to see what is going on. The result would be to have a code that is either not working or provides unexpected results.

If we try to apply the same example using let:

function myFunc () {
let x = 100;
let x = 200;
console.log(x);
}
myFunc ();

The output of the console will be: "Uncaught SyntaxError: Identifier 'x' has already been declared"

This error message makes you less prompt to errors and thus helps you to have the control of your code.

Moreover, the fact that let has block scope, makes the overall code more organized since variables only exist when they have to and not throughout the code.

Tip: Copy the above piece of code and paste it directly into the console, to see how it works.