Beginner JavaScript Interview QuestionsBeginnerConcept
JavaScript · Question 16
What is a callback function in JavaScript?
Direct answer
A callback is a function passed to another function so the receiving code can invoke it later or as part of its operation.
Functions are first-class values in JavaScript, so they can be stored in variables, passed as arguments, and returned from other functions. A callback takes advantage of that capability.
In [1, 2, 3].map(n => n * 2), the arrow function is a callback invoked by map. Event handlers and older asynchronous APIs also commonly use callbacks.