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

181
Visualizações
Newbie Questions on Scoping and Hoisting?

I am learning Javascript and I got into the hoisting/scoping part. I got some questions that I really hope somebody can help me to clarify and here is my example:

So these works normally due to hoisting - the firstName variable is in Global Scope or function can be called before we declare the function:

function test() {
  console.log(firstName);
}
const firstName = 'a';
test();

OR

const firstName ='a';
test();
function test (){
  console.log(firstName)
}

However, if I swap the order of firstName and test(), the code does not work anymore:

function test() {
  console.log(`${firstName}`)
};
test();
const firstName = 'a';

According to the hoisting, I thought the firstName variable is in the Global Scope. Thereby, the test function should have been able to look up at the variable firstName when it is executed.

But why in the case above, it does not work?

Is there anything wrong with my understanding of hoisting/scoping?

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

0

Variables declared with let and const are also hoisted, but unlike for var the variables are not initialized with a default value of undefined. Until the line in which they are initialized is executed, any code that accesses these variables will throw an exception.

MDN DOCS

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