Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!