---
title: "What is a callback function in Javascript?Give an example."  
description: "What is a callback function in Javascript?Give an example."  
author: "Amrita Bhattacharjee"  
published: 2023-03-28  
updated: 2023-03-28  
canonical: https://www.mindstick.com/forum/157609/what-is-a-callback-function-in-javascript-give-an-example  
category: "javascript"  
tags: ["javascript", "javascript framework", "javascript object"]  
reading_time: 2 minutes  

---

# What is a callback function in Javascript?Give an example.

What is a [callback function](https://www.mindstick.com/forum/157808/what-is-a-callback-function-and-how-is-it-used-in-javascript) in [Javascript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)?Give an example.

## Replies

### Reply by Amrita Bhattacharjee

A [callback](https://www.mindstick.com/articles/152/using-the-callback) [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) is a type of function that is passed being an argument to other function to get executed after the completition of the first task by main function.

This type of function is used to ensure that other code of another function gets executed only after the completion of the first ever task by main function.

The example code of callback function in Javascript is as follows:

```javascript
function doHomework(subject, callback)
{
  console.log(`Starting my ${subject} homework.`);
  callback();
}

doHomework('Math', function()
{
  console.log('Finished my homework.');
});
```

Here in the above example, the doHomework() function is used to take two arguments: subject for which a string is used to represent the subject of the homework function and callback refers to a function which will get executed only after the finishing of homework. Then the doHomework() function logs a specific message to console to indicate that it starts the homework and after the execution of homework then the callback function gets executed.

After the execution as starting of homework when the callback function gets executed then the output of the program will be as follows:

```javascript
Starting my Math homework.
Finished my homework.
```

Here the callback function gets executed after the homework function which has completed the task of the logging message to console.The example which is used here can be considered as a basic example of callback function in Javascript.


---

Original Source: https://www.mindstick.com/forum/157609/what-is-a-callback-function-in-javascript-give-an-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
