Search⌘ K
AI Features

Introduction to Callbacks

Explore how functions in JavaScript act like objects and how passing functions as callbacks allows you to run code asynchronously, reduce repetition, and manage event handling. This lesson helps you grasp the fundamental role of callbacks in creating flexible, powerful JavaScript applications.

We'll cover the following...

One of the most powerful properties of JavaScript is that functions are first-class objects. This means that they are like any other object and have the same properties as standard objects. In fact, we should think of them as nothing more than callable objects.

Like an object, we can store properties on a function.

Javascript (babel-node)
function fn() {};
fn.property = 'Hello!';
console.log(fn.property); // -> Hello!

An object can have any data type as a property. A function can as well. In fact, we can even set a function as a property on another function.

Javascript (babel-node)
function fn() {
console.log('Executing fn');
}
fn.func = function() {
console.log('Executing func');
}
fn(); // -> Executing fn
fn.func(); // -> Executing func

The point is that functions in ...