DEV Community

Cover image for Callback in Javascript
 Rahul Gupta
Rahul Gupta

Posted on

1 1

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....

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay