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
Edwin Carbajal
4,133 PointsFunction Callbacks
Can someone please explain how function callbacks work? I am a little confused. I would like to know how they work and also why we pass in a function as a callback. And what is the definition of 'callback'?
const example = function('something', () => {
// Do something here..
});
1 Answer
Joseph Frazer
5,404 PointsI will show you and explain how to make a callback function. First create your function with any parameter name:
function myFunction(callbackName) {
// Leave empty for now
}
Next we call the function in the function:
function myFunction(callbackName) {
callbackName();
}
Next lets call our magnificent function, but lets do something tricky:
function myFunctionTwo() {
console.log('Hello');
myFunction(myFunctionTwo);
}
Here we call our function in a function. It's an ordinary parameter, but in the function code, we call that parameter as a function. If you need me to explain it more in depth please ask me.