Bummer! You must be logged in to access this page.

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

what is JS callback function?

how is it different from any other function, in syntax or any other difference? i'll be obliged to any Video link on the subject.

2 Answers

A callback function is a function that is being called within another function. Meaning that when you call function one you pass your second function as an argument and it runs.

As you can see from the example below I have two functions X and Y. When I set my function y to accept an parameter, which in this case is another function. When I am calling function y I am passing function x as an argument.

Result in the console:

"I am called first" "I am called within the function"

function x(){ console.log("I am called within the function") }

function y(callback){ console.log("I am called first") callback() }

y(x);

Also check out this video on youtube : https://www.youtube.com/watch?v=pTbSfCT42_M&t=457s

thanks