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

208
Visualizações
simple function isn't calling the callback

const five = function() {
  console.log(5)
}

const func = function(callback) {
  console.log(3)
  callback
  console.log(7)
}

func(five)

I would like to know why I have in the output

3
7

instead of

3
5
7

I tried to put callback() with parentheses and I got an error, why?

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

0

Even though the function is assigned to a variable you need to call it as a function, with parentheses.

const five = function() {
  console.log(5)
}

const func = function(callback) {
  console.log(3)
  callback()
  console.log(7)
}

func(five)

about 4 years ago · Juan Pablo Isaza Relatório

0

The issue with your code is that you aren't calling callback with parentheses.

To invoke a function in JavaScript, you need to use parentheses at the end, even if you don't need to pass in any parameters.

So, change your callback line from this:

callback;

To this:

callback();

In summary, this is the fully working code.

const five = function() {
  console.log(5);
}

const func = function(callback) {
  console.log(3);
  callback(); // This line has been modified
  console.log(7);
}

func(five); // Should output: 3, 5, 7

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