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

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

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 Denunciar

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 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