Khi một hàm được truyền như đối số cho hàm khác, nó gọi là callback function. Hàm chứa sẽ gọi hàm callback khi hoàn thành tác vụ hoặc khi sự kiện xảy ra.
Ví dụ
<html>
<head>
<script>
var callback = function(myCallback) {
setTimeout(function() {
myCallback();
}, 5000);
};
document.write("First is displayed<br>");
document.write("Second is displayed<br>");
callback(function() {
document.write("This is Callback function<br>");
});
document.write("Last is displayed");
</script>
</head>
</html>