Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Callback Functions in JavaScript Introduction to Callback Functions Anonymous Functions

Tomasz Grodzki
Tomasz Grodzki
8,130 Points

I don't see difference between function and callback function, except syntax

Andrew said, that this function below could get executed at any point in the future.

executeCallBack ( function () {
    console.log('Hello');
});

Well... Next function, also could be invoke at any point in the future. So what's the point of using callback function? As far, everything that was said about callback I could do without callback. Launching function at some, specific point of program does require some e.g. conditional statements or event listener, what's the difference if I use callback or no?

 function sayHello() {
    console.log('Hello');
}

2 Answers

Steven Parker
Steven Parker
231,186 Points

A "callback" is simply a function that is passed to another function as an argument. The other difference is that you generally don't invoke callbacks yourself. That is done by the function that you pass it to.

It get's more clear with examples later on;

  • A function that calls a callback function at set time intervals, is one example.
  • A function that calls a callback function at certain user inputs, mouseClick or similar, is another.

The main take-away with callback functions is that they are functions that you can passively prepare in the code, to be called at a given point later in the runtime; a function will run when it is called - the callback can define when that call happens.