Javascript – How to Use Callbacks




a callback is a function that is passed as an argument to another function, this makes functions more flexible

the callback is invoked inside the function

you can also use an anonymous function as a callback

the use of callbacks is very common, and are used often

———————————————————–
http://atechapart.com | http://linkedin.com/in/alvinsanchez

see my codepens @ http://codepen.io/_asanchez/

Original source


34 responses to “Javascript – How to Use Callbacks”

  1. Thanks, I'm starting to get it, I've encountered it before and was left stumped. Examples, I could follow. I had to stop and think about what was being done, and that combined with the output removed some of the fog swirling around this topic. Thanks again.

  2. I think I got it .

    function person(name, gender, callback){

    N = name.toUpperCase();
    G = gender.toUpperCase();
    callback(N,G);

    }

    function show_person(arg1, arg2){

    console.log(arg1, arg2);
    }

    person("Ritchie", "male", show_person)
    person("Max", "male", show_person)

  3. Thanks for the video. Things start to get tricky when arguments are passed to the callback. I'm barely starting to understand this. I was wondering what you were gonna name the anonymous function then I was like oh yea duh lol.

Leave a Reply