Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda