DEV Community

Cover image for JavaScript Interview Questions
lassiecoder
lassiecoder

Posted on

JavaScript Interview Questions

What's callback?

  • Passing a function definition to another function as an argument is callback.
  • A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. ~MDN
function callbackFirst(arg1,arg2,arg3){
    console.log(arg1(), arg2(), arg3() );
};

callbackFirst( function callbackSecond(){
            return "Returned second callback";
        } , function callbackThird(){
            return "Returned third callback";
        } , function callbackFourth(){
            return "Returned fourth callback";
        } );
Enter fullscreen mode Exit fullscreen mode

The above function will return "Returned second callback Returned third callback Returned fourth callback" as output.

How to differentiate between synchronous and asynchronous callback?

Only setTimeout, setInterval, requests and events are asynchronous, while the other built-in callbacks are synchronous like Array.prototype.map

Top comments (3)

Collapse
 
mxldevs profile image
MxL Devs

A callback is when you meet someone nice at the party and offer your phone number with the instructions "call me :)" and then hope you get a call later.

Synchronous callback is when you sit there staring at your phone waiting for that call.

Asynchronous callback is when you're busy doing your own thing and forgot you gave that number in the first place.

Collapse
 
lassiecoder profile image
lassiecoder

Love teh way you explained it 👏🏻😃

Collapse
 
mxldevs profile image
MxL Devs

Thanks, I've been trying to improve my #ELI5