Technology Apr 15, 2026 · 2 min read

Callback Function in JavaScript

A callback function in javascript is a function passed as an arguement into another function and executed later. The benefit of using a callback function is that you can wait for the result of a previous function call and then execute another function call. How Callbacks Work Passing the Functi...

DE
DEV Community
by QUBE
Callback Function in JavaScript
  • A callback function in javascript is a function passed as an arguement into another function and executed later.

  • The benefit of using a callback function is that you can wait for the result of a previous function call and then execute another function call.

How Callbacks Work

  • Passing the Function: The function you want to run after some operation is passed as an argument to another function.

  • Executing the Callback: The main function executes the callback function at the appropriate time. This can be after a delay, once a task is complete, or when an event occurs.

Types of Callbacks:

  1. Asynchronous Callbacks
  • Asynchronous callbacks are executed at a later time, allowing the main program to continue running without waiting.

  • This is essential for preventing the application from freezing during long-running tasks like network requests.

  1. Synchronous Callbacks
  • Synchronous Callbacks are executed immediately within the outer function, blocking further operations until completion.

  • Array methods like map(), filter(), and forEach() use synchronous callbacks.

Example For Callback Function:

Avoid callbacks when:

  • Code becomes nested and unreadable (use Promises or async/await).

  • You need error handling in asynchronous operations (Promises are better).

References:

How to Use Callback Functions in JavaScript

When you're building dynamic applications with JavaScript that display real-time data – like a weather app or live sports dashboard – you'll need a way to automatically fetch new data from an external source without disrupting the user experience. Yo...

favicon freecodecamp.org

JavaScript Callbacks - GeeksforGeeks

Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.

favicon geeksforgeeks.org
DE
Source

This article was originally published by DEV Community and written by QUBE.

Read original article on DEV Community
Back to Discover

Reading List