Review: Using JavaScript Objects

Objects are an essential data type of JavaScript, and learning how to use them is necessary to develop applications. We will start with a quick review of objects from the first course. Try the examples in this article to review creating and manipulating objects, accessing objects using dot [.] notation and bracket notation, and setting and getting object members. You can use the JavaScript Console on your browser DevTools to complete the examples.

You've been using objects all along

As you've been going through these examples, you have probably been thinking that the dot notation you've been using is very familiar. That's because you've been using it throughout the course! Every time we've been working through an example that uses a built-in browser API or JavaScript object, we've been using objects, because such features are built using exactly the same kind of object structures that we've been looking at here, albeit more complex ones than in our own basic custom examples.

So when you used string methods like:

myString.split(",");

You were using a method available on a String object. Every time you create a string in your code, that string is automatically created as an instance of String, and therefore has several common methods and properties available on it.

When you accessed the document object model using lines like this:

const myDiv = document.createElement("div");
const myVideo = document.querySelector("video");

You were using methods available on a Document object. For each webpage loaded, an instance of Document is created called document, which represents the entire page's structure, content, and other features, such as its URL. Again, this means it has several common methods and properties available.

The same is true of pretty much any other built-in object or API you've been using - Array, Math, and so on.

Note that built-in objects and APIs don't always create object instances automatically. For example, the Notifications API - which allows modern browsers to fire system notifications - requires you to instantiate a new object instance using the constructor for each notification you want to fire. Try entering the following into your JavaScript console:

const myNotification = new Notification("Hello!");