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.

Dot notation

Above, you accessed the object's properties and methods using dot notation. The object name (person) acts as the namespace - it must be entered first to access anything inside the object. Next, you write a dot, then the item you want to access - this can be the name of a simple property, an item of an array property, or a call to one of the object's methods, for example:

person.age;
person.bio();


Objects as object properties

An object property can itself be an object. For example, try changing the name member from

const person = {
  name: ["Bob", "Smith"],
};

to

const person = {
  name: {
    first: "Bob",
    last: "Smith",
  },
  // …
};

To access these items, you need to chain the extra step onto the end with another dot. Try these in the JS console:

person.name.first;
person.name.last;

If you do this, you'll also need to go through your method code and change any instances of

name[0];
name[1];

to

name.first;
name.last;

Otherwise, your methods will no longer work.