There are nine units in Lambda School’s Full Stack Web Development Pre-course. I’m going to summarize what the pre-course covers for each of them. This material is basically ground-floor information that students need to know before they start the full course.

Callbacks
A callback is essentially the ability of a function to be passed to another function as an argument. Its something like a glorified version of passing a variable to a function so that its value can be used in the function its being sent to.
Common convention uses the word “callback” or the letters “cb” as the variable for the function being used as a variable in another function. Its not set-in-stone, but its essentially a best practice.
As per the Lambda School text:
A very important concept in Javascript is the ability to pass a function as an arguments to another function. These functions are called callbacks. These functions can be called anytime and passed arguments within the function. We will soon discover why callbacks are so important to Javascript. Convention is to use the callback or cb argument variable as your callback, although as always you are free to name them what you please.
function saysHelloToUser(user) {
return ‘Hello ’ + user + ‘!’;
}
function saysGoodbyeToUser(user) {
return 'Goodbye ' + user + '!';
}
function createGreeting(user, cb) {
return cb(user);
}
createGreeting('Dan', saysHelloToUser); // 'Hello Dan!'
createGreeting('Dan', saysGoodbyeToUser); // 'Goodbye Dan!'
Looking at the code above, we can see that three functions have been declared. One says “hello” to a person, one says “goodbye” to a person, and the third looks for a person’s name and initiates a callback.
The two lines of code at the bottom use the third function to call the first function that says “hello” and passes a person’s name to it. That “hello” function then returns the person’s name and a string as output.
The last line of code does the same thing, but uses the second function to say “goodbye” instead of hello. Although these functions simple, its evident that much more complex functions can be used instead, and their output can be used as data to power other functions via callbacks.
Here’s the explanation for that same code, from the Lambda School text:
Within createGreeting we have set the parameter cb to equal whatever was passed in as the second argument. In the above example we see that in the first createGreeting call, the function saysHelloToUser was passed as the second argument. This gives us the ability to call cb (which is equal to the function saysHelloToUser) within the createGreeting function.
One thought on “Lambda School Pre-course 22: Learn to demonstrate understanding of and implement callbacks”