Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
"Why" JavaScript hoists "let" and "const" variable and keep them in temporal zone? so what is the benefit for this behavior?

Why javascript do hoist on let and const variables and we can't reach them until the initialization and in this way, end temporal dead zone. Is there a particular benefit for this behavior? why js doesn't leave those without any hositing on those?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Because when you shadow a variable, it would be unclear to which variable an identifier would refer in the temporal dead zone, especially as variables declared with var are hoisted too. As such let and const are consistent with var as they are also hoisted, but they're more restrictive in situations where var has shown confusing behavior.

 let val = "a";
 {
    console.log(val);
    let val = "b";
 }
about 4 years ago · Juan Pablo Isaza Relatório

0

Programming languages generally have a tradition like variables should be defined at the top. Behind the scenes JS is a part of this tradition too. It hoists var to the top and sets it to undefined until it's defined. This behaviour might lead unwanted consequences because JS will attempt like it's just a normal variable and try to do something with that undefined however since there is nothing you will get an error. So they introduced let and const keywords to prevent this bad behaviour and said these are also hoists and we set them to uninitiliazedand you can't use them until they're assigned to something because that has no meaning, why would you want to use a variable before it assigned to something?

about 4 years ago · Juan Pablo Isaza Relatório

0

wall it makes things more clear -sometimes you have to declare the same name of variable multiple times and you don't want to mix up their values

-when you use var it declares on a global scope and after function runs successfully it doesn't clear meemory it stay there in this case if you have many many variables that require too much memory is going to affect on your app performans

in your case:-

     let val = "a"; // this let have globle scope
 {
    console.log(val);
    let val = "b"; // this let have local scope that going to be clear from memory when this brakets endes 
 }

Conclusion:-

var is a global scope
let & const are BLOCK scope that going to be clear after brackets ended
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda