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

87
Vistas
var hoisting and scope availability

As we know var declarations get hoisted but with undefined value:

console.log(a); // undefined

var a = 2

But this code is different:

function foo() {
    console.log(a); 
}

function bar() {
    var a = 3;
    foo();
}

var a = 2;

bar(); // 2

Wy does it log 2 instead of undefined ?

Why var a's value became magically accessible before it's declaration ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You're calling bar() after you initialize the variable, so it's not undefined any more.

See this simpler example:

function foo() {
    console.log(a); 
}

foo(); // a is not yet initialized, prints undefined

var a = 2;

foo(); // a is now initialized, prints 2

Hoisting just makes the variable declaration available (so you don't get an "Undefined variable" error). The initialization is just an assignment that happens in the normal order.

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