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

87
Visualizações
How is variable declaration happening in javascript?
console.log(x)
var x = 10;

The console gives me undefined but I was expected reference error

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

0

var is the old way of declaring a variable in JS. It has a few properties that you should consider in comparison to what let and const let you do.

Your variable declaration var x = 10 will be raised up to the top of the script, as a function declaration on the other hand is hoisted by the JS engine during compilation. However, ONLY the declaration is hoisted and the variable will be assigned undefined until you assign to it another value manually using the assignment operator =.

JS engine doesn't throw a ReferenceError because your code is translated (using hoisting) into

var x;
console.log(x);
x = 10;

At the same time, var doesn't have the concept of block scope, so at the same time the code below is running just fine

console.log(x);
{
  var x = 10;
}

The code above is being translated (using hoisting) into

var x;
console.log(x);
{
  x = 10;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

If a variable is used in code and then declared and initialized, the value when it is used will be its default initialization (undefined for a variable declared using var, use const and let to avoid this).

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