FacebookTwitter

What is a callback function?

By on Jul 13, 2014 in Definitions

Share On GoogleShare On FacebookShare On Twitter

Many people out here are confused by what a callback is. Perhaps the name makes it hard to understand. But in reality callbacks are very simple.

A callback function is a function that is passed as an argument of another function and it is invoked after some kind of event. The name “call back” can be attributed to the nature of the function, since once its parent function completes, the function that got passed as an argument gets called. In other words, the parent function “calls-back and executes the function passed as an argument.

As you can already imagine, callbacks are extremely useful. They allow for you to call a function within a function. Here you can see some examples:

?View Code JAVASCRIPT
1
2
3
4
5
$("button").click(function(){
  $("p").hide("slow",function(){
    alert("The paragraph is now hidden");
  });
});

In this example. Whenever you click a ‘button’ all ‘p’ tags will call the hide() function with parameters “slow” for the speed in which the ‘p’ tag should hide and a second parameter a function, which task is to ‘alert’.

Enmanuel Corvo

Enmanuel Corvo

Software Engineer at LiveAnswer
Enmanuel Corvo is a Software Engineer experienced in Web Applications Development. Huge fan of WordPress and an active plug-in author who enjoys writing code and blog posts about anything related to technology.
Enmanuel Corvo

Latest posts by Enmanuel Corvo (see all)