What is callback?

What is a callback, its importance and also callback hell is it good or bad how to deal with it?

Callback are the function that user pass into another function.

Example-

function f(callback){
console.log(“This is main function”);
callback();
}

function f1(){
console.log(" f1 Called");
}

function f2(){
console.log(“f2”);
}

f(f1); // Here function “f1” is called as an argument in function “f”
So Output will be
This is main function
f1 Called

Similarly function “f2” can be called in function “f”
f(f2);

I think function callback is good practice as it gives more usability and throw less error .

Like in above example if we call some random function say f(Math.floor); So Instead of throwing error it will successfully get executed & print only “This is main function”.

1 Like

Your last point f(Math.floor) is little bit confused so if it doesn’t throw any error then how will you solve bugs in your program??

The callback is a life saviour when we are dealing with the asynchronous nature of javascript…
Callback is use full if we want to use one function value in another function without using return key word…

2 Likes

Here on passing Math.floor I mean to say that suppose someone passed wrong parameter and it has no relation with the output of that function . So instead of throwing some random errors it will simply execute rest part.

So the problem is if you pass some wrong value it will hard to identify what’s going wrong in the code

Any function that is passed as an argument is called callback function

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.

  • A callback function is a function that is passed as an argument to another function, to be “called back” at a later time. A function that accepts other functions as arguments is called a higher-order function , which contains the logic for when the callback function gets executed. It’s the combination of these two that allow us to extend our functionality.

Example:

function createQuote(quote, callback){ 
  var myQuote = "Like I always say, " + quote;
  callback(myQuote); // 2
}

function logQuote(quote){
  console.log(quote);
}

createQuote("eat your vegetables!", logQuote); // 1

// On console: 
// Like I always say, eat your vegetables!
1 Like

A callback is a plain JavaScript function passed to some method as an argument or option. It is a function that is to be executed after another function has finished executing, hence the name ‘ call back ‘. In JavaScript, functions are objects. Because of this, functions can take functions as arguments, and can be returned by other functions.

Without thinking too much, see the following example. In the following example, I just call the print function from the add function.

function print( ans ){
    console.log(ans) ; // 7
}
function add(a, b){
    print(a+b) ;
}

add(2,5);

What if I use the print function as a parameter? Without using print function from global scope I just pass the print function as an argument.

function print( ans ){
    console.log(ans) ; // 7
}
function add(a, b, callback){ // here callback = print
    callback(a+b) ;
}
add(2,5,print); // print function as a parameter 


Generally, JavaScript allows function as a parameter.

So any function that is passed as an argument is called a callback function. I think now callback is understandable to you

your good information and good question but i am not idea.