What are callback functions in JavaScript?

Learn how callback functions work and why they are essential for asynchronous programming in JavaScript.

beginnerFundamentalsjavascriptcallbacksasynchronous
Published: 11/3/2025
Updated: 11/3/2025

Question

What are callback functions in JavaScript?


Answer

A callback function is a function passed as an argument to another function to be executed later.

function greet(name, callback) {
  console.log('Hi ' + name);
  callback();
}

function bye() {
  console.log('Goodbye!');
}

greet('Ghazi', bye);

Callbacks are heavily used in asynchronous APIs (like event handlers, setTimeout, and file operations).

Real-World Example

setTimeout(() => {
  console.log('Data loaded!');
}, 2000);

Quick Practice

Write a function that fetches data (simulated by setTimeout) and logs a message when it’s done.

Summary

Callbacks let JavaScript handle asynchronous behavior — they’re the backbone of non-blocking code.

Related Videos
Watch these videos to learn more about this topic
Frequently Asked Questions

Are callbacks still used today?

Yes, but mostly replaced by Promises and async/await for better readability.


Stay Updated

Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.