Function Callbacks in JavaScript
Explore the concept of function callbacks in JavaScript and how to pass functions as arguments. Understand how callbacks enable asynchronous behavior and learn defensive programming techniques to prevent runtime errors by verifying argument types before invocation.
We'll cover the following...
Introduction to function callbacks
One of the most powerful features of JavaScript, and in fact, the technology that NodeJS was built on, is the concept of callback functions. A callback function is a function that is passed in as an argument to another function and is then generally invoked within the original function. In other words, we are calling a function and telling it to go and do what it needs to do, and when it is finished, to call the function that we have supplied.
Passing a function as an argument
Just as we can pass a value into a function, we can also pass a function into a function as one of its arguments. ...