DEV Community

Milecia
Milecia

Posted on • Updated on

What is a Callback Function?

There are still a bunch of different types of functions out there. You already know about arrow functions, but what about callback functions? Do you know what they are?

Callback functions are talked about a lot, but what exactly is a callback function? To keep it short, a callback function is basically a function that gets called when something happens. For example, when someone submits a form on your website, you need to run validations which means there are some functions that need to run in the background.

So when the user submitted the form, that action triggered the callback function that started all of the validations. If you weren't using a callback function, your code would get hung on the validations and it would just sit there until someone submitted a form. That's definitely not the way you want your website to behave.

A callback function let's you run through your code until an event triggers it. Then the callback is run and then you go back to your original function. Here's an illustration of a callback function in action:

callback function

You can see that the callback is waiting until something happens before it does anything and as soon as the callback function returns the code goes back to doing stuff. That's really all there is to the callback function. Yes, that really is all it is.

Hopefully this helped clear up some of the jargon that you hear developers and programmers in general use. Just remember that most of these convoluted sounding terms are really just simple things being dressed up to look fancier.


Hey! You should follow me on Twitter because reasons: https://twitter.com/FlippedCoding

Top comments (1)

Collapse
 
mohaymin profile image
Mohaymin

I tried reading about the arrow function but the link does not work. Can you please update the link?