Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda