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

206
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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