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

105
Visualizações
Why this Immediately Invoked Function returns number?

I want to understand why this code returns number:

(function f(f){
    return typeof f();
})(function(){return 1;});

First I assume it will return undefined since function f executes before the last function, but then I run the code with console logs and the result was number. Anyone would mind explaining why this was the output?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

f inside the first function’s body refers to the parameter called f. An equivalent version without the confusing shadowing would be:

(function f(g){
    return typeof g();
})(function(){return 1;});

And an equivalent version with the IIFE separated into a function definition and a call:

function f(g) {
    return typeof g();
}

f(function () {
    return 1;
});

(If it’s not clear why this returns 'number', you should review the concept of functions as values before jumping into intentionally more complicated IIFEs with shadowing that also make use of the concept.)

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