Callbacks are a key feature in JavaScript and allow you to run a function after another function has finished execution, hence the term “call back”.
To understand this, let’s first define a simple function:
```
function greeting(name) {
alert(‘Hello ‘ + name);
}
```
This function will just simply print out `Hello ` followed by whatever the `name` parameter is.
Now, let’s create another function that takes two parameters:
- a value `name`
- a function `callback`
```
function processUserInput(callback) {
var name = prompt(‘Please enter your name.’);
callback(name);
}
```
In this function, `callback` is being invoked right after `name` variable is assigned a prompt value. Part of the beauty of JavaScript is that it’s an interpretive language, which means callback can be any valid function that’s been defined:
```
processUserInput(greeting);
```
In this case, `greeting` function is a callback function. After you input your name, the script should alert ‘Hello, your_inputted_name’.
The advantage of using callback function is that they make sure a function doesn’t run before the first one is finished. So callbacks ensure correct order of execution. They are useful when you need to perform something after a certain task is done.