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

378
Vistas
Why does JS function show in block scope if it is hoisted in global scope?

I was playing around with JS scopes to get a better understanding of the concept. Doing that I wrote the following script:

{
  let a;
  function x() {
    let p;
    var q;
  }
}

x();

Surprisingly, when debugging in the Chrome dev console, I realized that if I stop the script at line 2 or let a, I can see the block scope having both variable a and function x as visible in screenshot 1.

Also, on logging window.x outputs undefined However, as soon as I jump on to the next breakpoint which is line 4 or let p, the block scope is no longer there and a local scope is created. Now, on logging window.x outputs the function definition.

Now as I understand, since I am able to call x() from the global scope means that the function declaration is available in the global scope. Then why isn't the function binding available before the creation of function's execution context (if it is hoisted to global scope)? And why does writing x() at the top of the block output TypeError: x is not a function?

screenshot 1

Note: I am not writing functions like this anywhere, i just wrote this script to test my understanding of scopes. Apologies if this is a wrong way of writing functions.

EDIT per comments:

Chrome and FF show undefined while Safari shows function definition.

console.log(x); // logs undefined
{
  let a;
  function x() {
    let p;
    var q;
  }
}

x();

about 4 years ago · Juan Pablo Isaza
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