Nearby lessons

16 of 22

JavaScript: Callback Function

📌 What are Callback Functions?

A callback function is a function passed as an argument to another function, and it is executed after the completion of that function. Callbacks are widely used in asynchronous JavaScript.

1️⃣ Basic Callback Function Example

This example shows how to pass a function as a parameter and call it after an action.

Example02
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

2️⃣ Callback with setTimeout (Asynchronous)

Callback functions are commonly used with asynchronous methods like setTimeout().

Example03
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

3️⃣ Callback Inside Another Function

This shows how to execute a callback function after completing a task inside another function.

Example04
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

4️⃣ Anonymous Callback Function

You can also use anonymous functions as callbacks without naming them separately.

Example05
🚀Code Cell
Loading Editor...
✏️ You can edit this code — click Run to refresh the preview.
Output

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3