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

156
Vistas
Variable hoisting and function overwrite

I'm trying to understand the example below:

(function (){ 
    var func = function(){
        console.log('foo')
    }
    function func(){
        console.log('bar')
    }
    func();
})();

Why does this prints foo and not bar? Does it has something to do with variable hoisting?

I'm also confused by the code below:

(function (){
    function func(){
        console.log('bar')
    }
    var func = function(){
        console.log('foo')
    }
    func();
})();

My logic says, that JavaScript will execute the first function it sees, but the output shows something else. Why this still produces the same output as the first example? Am I missing something?

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

0

Function declarations are hoisted. Assignments are not.

In the first case, the declaration is hoisted and then overwritten by the assignment.

In the second case, the declaration is the first thing in the function (so hoisting makes no difference) and then still overwritten by the assignment.

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