A callback function is invoked inside another function to complete some sort of routine or action and you can also pass arguments to it if it requires some. Also, a function can take one or more callback functions as parameters.
In the following example, we have a counter and a button. Upon click of a button, we will pass myFunction1() as a callback function to myFunction2() which in turn will increment the counter. Please have a look over the code example and the steps given below.
HTML & CSS
We have 3 elements in the HTML file (div, button, and h1). The div element is just a wrapper for the rest of the elements.
The button element and the h1 element have “Click Me” and “Counter” as innerText respectively.
We have done some basic styling using CSS and added the link to our style.css stylesheet inside the head element.