DEV Community

Cover image for Callback in Javascript
 Rahul Gupta
Rahul Gupta

Posted on

Callback in Javascript

A callback function is a function passed into another function as a argument. The function is invoked in the outer function to complete an action.

Let’s take a simple example of how to use the callback function:

`function callbackFunction(message) {
console.log("Hello " + message);
}

function outerFunction(callback) {
let message = prompt("Welcome to The Coding Dev");
callback(message);
}

outerFunction(callbackFunction); `

Top comments (1)

Collapse
 
frankwisniewski profile image
Frank Wisniewski

With this explanation of a callback function, I wonder why I should use it....