JavaScript callback explained in simple words

JavaScript callback explained in simple words
Reading Time: 1 minute

JavaScript callback is the first class function which is passed as an argument to any function. In JavaScript, we can pass variable and function both as an argument. We can execute the function inside the other function. JavaScript and jQuery are heavily based on callback. There will be the frequent requirement in jQuery where you will have to use the callback. Although, this is a very simple concept but still most of the books cover this in advance topic.
Suppose, you have two actions to perform and one action is dependent on the other. In this case, you have to wait for one action to perform then execute the other action. In this scenario, we use JavaScript callback.

The basic example of the JavaScript callback

In the above example, you can see I have called the first function and passed the second function in the argument. So, it will execute the first function and then it will execute the second function. You can place your callback according to you need. You can place it on top if you want to execute it first and then the body of the function or you can place it in the end if you want to execute after the completion of the function.

Pass arguments in the callback function

In the above example, you have seen how we can pass parameter in the callback function. We have created one function where we got the input and pass those input to the callback function. It’s very simple to use.

Leave a Reply

Your email address will not be published. Required fields are marked *