Sitemap
Daniel Chan

Daniel Chan

Software Developer

Press enter or click to view image in full size

Breaking down the Callback Function

5 min readMay 12, 2022

--

I remember learning about callback functions as if it were yesterday. I was confused, overwhelmed, and even frustrated as I first attempted to grasp at how it all worked. If you are here reading this, you are probably in the same boat. Truth is, the callback function is many student’s first tough-to-understand concept while learning how to code.

Why is that?

It is because callbacks are one of the first fundamentals of Javascript that break the linear thought process that people are so use to. It introduces a programmer’s method of thinking, a more modular approach. I found the best way to get it to “click” was to walk through it in steps and break it down conceptually, so let’s do that together.

What is a callback function?

A callback function is a function that is passed as an argument for another function. The “inner function” (the callback) is the function which is passed as the argument to the “outer function”

Let’s start with some simple examples first distinguishing a normal function from a callback function:

Press enter or click to view image in full size

In this first example we have a normal function which contains a parameter. When invoked below, the argument “Daniel” is passed into the parameter. Callbacks work similarly, where instead of the string argument, we are passing in another function to the parameter.

Press enter or click to view image in full size

Here in the second example we can see how “theCallback” is a function. This function is passed into the second parameter of the outer function when the “greeting” function is called on line 10. It is important to remember that “theCallback” is being passed as an argument into the parameter “callback”, as this part first confuses most people. The parameter does not require the parentheses () which invokes the function, as is not being invoked at that point, but rather it is just storing the value and being the placeholder for the callback so that it can be used within the higher order function. We can see it is later invoked when it is called at the end of the function.

Anonymous Callback Functions

Callback functions can also be anonymous functions, that is, they don’t have to be assigned to an identifier. Anonymous functions are often used when a callback is only needed to be used in one place.

Press enter or click to view image in full size

In the example above, we can see how an inner function is called as the first argument within the “setTimeout” function. This inner function does not have a variable which stores its value for use at a later time.

We can see in the normal callback situation, the inner function is assigned to the variable “timedAlert” and can then be passed as the first argument to “setTimeout”, and again elsewhere if needed, by simply calling on “timedAlert”. It is best practice to use anonymous functions when we only need a function to run in one place.

When are they used?

The most common use of callback functions are during iterations and event listeners. But honestly, you will quite literally see them all the time with anything JavaScript.

Get Daniel Chan’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s look at some examples below and see if you can spot them.

Iterations

Press enter or click to view image in full size

Can you spot where the callback is located in the example above? If you said within the forEach function then you are correct! Using the callback in this situation allows our forEach function to console log an output for each element in our array. The forEach function loops through our array, taking each element one at a time, and passes it as an argument to our callback function! Our callback returns the logged command with each element, in this case, each friend name.

Event Listeners (DOM)

Press enter or click to view image in full size

An event listener is used to look out for a specific event, such as a click or a keyboard event, and performs one or more actions when it said event happens. With an event listener, callbacks are always passed as the second argument within the function. This callback function runs following the occurrence of the event that which the event listener is looking out for. In our example, our event listener waits for a “click” on a “button”. When clicked, the string ‘button clicked’ will be logged.

Why are callback functions important?

Now that we have seen a few examples of how callbacks are used, you might have the question, why are they so important?

Callback functions are important for asynchronous programming. Once we begin throwing all our code together, it is important our code can run without running into bugs and other errors. If you used a function synchronously instead of in a callback, then the function could be called before you get the data that the function needs and it would cause issues that require debugging.

They are also important in keeping our code clean and easy to read. In some instances you may begin to need to nest functions into functions over and over again. Callback hell shown below is an example of this:

Press enter or click to view image in full size

It should be good practice to avoid deeply nested functions, but if it is necessary, assigning a variable to a callback can clean our code up and makes things more understandable compared to the image above.

Congrats, you now know callback functions!

I hope this was enough to give a basic understanding of callback functions! Callbacks are an important part of JavaScript and is used in many different ways to clean up and modularize our code. Overall, it is just a fundamental and vital concept to understand while learning JavaScript as it is used in other concepts like iterations and event listeners.

If you didn’t get it this time around, that is okay. Callbacks are a confusing topic, but you will learn it if you give it the time! Be patient with yourself and sometimes a different method of consuming content may help. I will link a good video resource below which has helped me conceptualize callback functions. Good Luck!

--

--