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
Tiffany White
5,373 PointsLost, still, as to what a callback function is.
I have been researching callback functions in JavaScript for some time and I still don't know how to recognize one in code. I am also not quite sure what they do.
Can anyone help?
1 Answer
Ford Heacock
18,068 PointsHey Tiffany! What helped me understand callback functions was learning that they are just functions being passed to other functions as a parameter. A very common one would be event listeners. Say for example you want to listen for a click event on a div:
myDiv.addEventListener('click',function(){
alert('you clicked me!');
});
Notice the anonymous function being passed in after 'click'? This is a callback function as it is a function being passed as a parameter to yet another function (addeventlistener).
Here's a great article to read that helped me a lot: http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/