To begin, it is worth mentioning some concepts:
The declaration of variables of the type var
can be a big problem for the simple fact that it can be changed regardless of where it is, see in the example, I left some comments with explanations:
var a = 1; //aqui a variável é declarada e iniciada com o valor 1
console.log(a)
for(a; a<3; a++){ //aqui, é pego a mesma variável, e no loop, é somado o valor 2, resultando em 3
console.log(a)
}
function r () {
return a+ 1; //aqui, a mesma variável, agora com o valor 3, irá ser somada com +1, resultando em 4
}
console.log(r())
Having this problem of statements, the staff saw the need to create variables that would take their value wherever it was, this is the case of let
. With the let
, if we declare him inside a função
, the same will be available only in it, without hosting occurrence out of it, example:
var r = 1;
function result () {
let r = 1+1;
return r;
}
console.log(r); //aqui irá exibir 1, pois estamos chamando a variável global
console.log(result()); //aqui é exibido o valor 2, pois foi chamado a função que contém a variável di tipo let
The variable const
has the same behavior as let
, what changes is that once declared, its value cannot be changed, example:
const a = 1;
a = a+2;
console.log(a)
In this example, in the output the error will be displayed "Assignment to constant variable"
meaning that a value is being assigned to the type variable const
, what is not possible.
CONCLUSION
Thanks to Hoisting, variables declared with the keyword var
may be used even before your declaration.
On the other hand, variables created with let
can only be used after your declaration, because, despite being high, they are not initialized.
In addition to variables declared with var
we have the possibility to use constants through the keyword const
or use block-scoped variables via let
t .
I personally recommend that you always use const
, because it leads to fewer mistakes. I still don’t see a situation where I need to use var
.
As a general rule, use let
only for loop counters or only if you really need reallocation. Anywhere else, use const
.
Possible duplicate of var, const or Let? Which to use?
– Marcelo Batista
@Marcelobatista What is the relationship of the question with Hoisting to be duplicated?
– Sam
Maybe of that
– bio
As far as I know there is no such thing as
let
andconst
. This argument is indicated in documentation although it does refer to ES5 "... In Ecmascript 2015, Let bindings are not Subject to Variable Hoisting, which Means that Let declarations do not move to the top of the Current Execution contex..."– Isac
@Isac So it is... there are places and people who say yes... my question was to know the difference between Isting before ES6 and after. But they closed the question by relating to another that has nothing to do with the question.
– Sam
It is interesting that although the documentation refers to what I said, that there is no
let
, there are posts on Soen saying precisely the opposite.– Isac
@dvd is a very interesting mechanism the Hoisting because thanks to it it is possible to use a variable or function before the declaration. You want to understand how this is done internally, it will be this?
– gato
@cat Before ES6 I know how it works. I still don’t understand how it works in ES6, with Let.
– Sam
Yesterday I spent all morning researching this and I did not come to a clear conclusion. Then today I come here to try a help and close the question. You will understand.
– Sam
Is your doubt even about Hoisting, or about function scope vs block scope?
– bfavaretto
@bfavaretto Dude, it’s really about Hoisting. About scope it’s already clear to me.
– Sam