Callbacks in JavaScript

Ashutosh K Singh
The Startup
Published in
2 min readNov 1, 2020

--

Photo by Christopher Gower on Unsplash

In the previous post, Synchronous vs Asynchronous Programming, we discussed the difference between Synchronous and Asynchronous programming in JavaScript. In this article, we will discuss what are Callbacks in JavaScript.

Callbacks

Callback is a function that is passed as an argument to another function and its execution is delayed until that function in which it is passed is executed.

Meaning, if I pass a function, let’s say function1 to another function, i.e., function2,then function1 will not be executed until function2 is executed.

Callbacks are a fundamental part of Functional Programming, and you may be already using them without knowing; methods like .map(), .filter(), and.reduce() all make use of callback functions. Let see an example:

In the above example, we iterate through the array arr, using the .map() method that accepts a callback function to modify the array elements.

In the above example, Function Declaration is used to define the function; you can also use arrow methods to pass a callback…

--

--