JavaScript Interview Questions
lassiecoder

lassiecoder @lassiecoder

About: I've been a Software Developer for about 5 years, focusing on mobile and web apps, more into JavaScript and React ecosystem.

Location:
Bangalore
Joined:
Dec 29, 2019

JavaScript Interview Questions

Publish Date: Jul 10 '20
7 3

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

Comments 3 total

  • MxL Devs
    MxL DevsJul 15, 2020

    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.

    • lassiecoder
      lassiecoderJul 18, 2020

      Love teh way you explained it 👏🏻😃

      • MxL Devs
        MxL DevsJul 18, 2020

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

Add comment