This course has been updated! We now recommend you take the JavaScript: The Hard Parts, v2 course.

Check out a free preview of the full JavaScript: The Hard Parts course:
The "Callback vs Higher-order Functions" Lesson is part of the full, JavaScript: The Hard Parts course featured in this preview video. Here's what you'd learn in this lesson:

After explaining the differences between callback function and a higher-order function, Will reviews their benefits: simplify the code, keep code DRY, and allow the running of asynchronous code.

Get Unlimited Access Now

Transcript from the "Callback vs Higher-order Functions" Lesson

[00:00:00]
>> Which of these was our callback, and which of these was our higher order function? Katy? Just call on Katy. Katy, which is our higher order function, would you say?
>> The
>> And which is our callback?
>> The multiply by two.
>> Yeah, and the placeholder for all those instructions, exactly.

[00:00:19] Our higher order function. We hear this esoteric sounding term, this obscure sophisticated higher order functions. This is a function that can take in another function. That's it. We've seen everything. This is higher order function. And our callback function is a little baby function that can be passed in as an input.

[00:00:37] Higher order function, and it's a higher order function because it It's so grand, it can have other functions inside it, that's why. It's of a higher order. It can have whole other functions, like a level above. Callback function is the little baby one that gets passed in. So the outer function that takes in the function is a higher-order function.

[00:00:56] Function we passed in is our callback function. Just to note, higher-order functions, I said takes in a function or, by the way, return out a function. Any function that can take in a function or takes in a function or returns it out, by definition is known as a high-order function.

[00:01:13] It's just a term we use to describe. We don't need to declare it a high-order function. You just declare it a function, if it takes in a function or returns out a function It's a high order function. That's it. So, call backs and higher order functions simplify our code and keep it dry.

[00:01:32] Look at this, before we had an independent function, individual copy of a ran too.And the only thing that was changing each time was his little operator and operand. That we applied to each element of the array. What a waste of all that code we're writing, to get a new function for each time.

[00:01:53] So instead, we create a generalized higher-order function, where we leave a placeholder. And all the repeated code is always in that general single version of the function. Leave a placeholder, and only when we invoke, call, execute the function, do we fill in What exactly we are going to do to each element of the array.

[00:02:12] And that's it. We determined our data, when we call our function. And we determined some of the specifics. The only bit that changes each time, the specifics of our functionality only when we call, invoke, execute our higher order. More general, parametrize, such as me putting a place holder.

[00:02:52] Not here, here it lies tomorrow morning. Tomorrow morning we will see the other powerful impact of call backs, which is in a synchronous JavaScript tomorrow morning. But for now, we move on. We've done principles of JavaScript, the thread, the execution context, the global one, the one we created when we call a function The cool stat that keeps track of all of these guys, now we've then seen how those principles can be used to execute higher order functions.

[00:03:25] And then pass in a callback and execute a callback inside, now we reach closure.