Question: I realise this is more of a general question, but I've read through similar answers on here but I can't find more of an overview. I'm new to callbacks and I'm trying to understand when they should be used.

The MDN web docs has this example;

```
function greeting(name) {
  alert('Hello ' + name);
}

function processUserInput(callback) {
  var name = prompt('Please enter your name.');
  callback(name);
}

processUserInput(greeting);

```

However I'm struggling to see how this is more beneficial than the following, where I'm not passing the greeting function as a parameter?

```
function greeting(name) {
  alert('Hello ' + name);
}

function processUserInput() {
  var name = prompt('Please enter your name.');
  greeting(name);
}

processUserInput();

```

Answer: 
As Javascript is async, sometimes it is difficult to handle response from non-blocking functions, for ex. if you are making an ajax call then it'll be executed asynchronously and results will be returned sometime later, by that time the main execution flow will pass the ajax code and starts executing following statements, in that case, its very difficult to catch the response to process further.

To handle those cases, callbacks comes into picture where you pass a function as the parameter to the ajax function and once the response is returned then call the callback by passing response data as a parameter to process further.

more info here <http://callbackhell.com/>
This example is not appropriate for understanding callbacks

In simple Language callbacks functions are used when we have to do some stuff **after** or in response of some other event or function or expression.
i.e when the parent function completes its execution then callback gets executed. 

simple Example

```
function hungerStatus(status,cb){
    return cb(status)
}
function whatToDo(status){
    return status ? "order Pizza" : "lets play"
}

hungerStatus(false,whatToDo)

```

Another example 

```
// global variable​
​var allUserData = [];
​
​// generic logStuff function that prints to console​
​function logStuff (userData) {
    if ( typeof userData === "string")
    {
        console.log(userData);
    }
    else if ( typeof userData === "object")
    {
        for (var item in userData) {
            console.log(item + ": " + userData[item]);
        }
​
    }
​
}
​
​// A function that takes two parameters, the last one a callback function​
​function getInput (options, callback) {
    allUserData.push (options);
    callback (options);
​
}
​
​// When we call the getInput function, we pass logStuff as a parameter.​
​// So logStuff will be the function that will called back (or executed) inside the getInput function​
getInput ({name:"Rich", speciality:"JavaScript"}, logStuff);

```

refer [callback exaplanation](http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/)