Lesson 27 +10 XP

Object Basics

Object Basics

An object is a collection of key-value pairs called properties. Real-world things are modeled with objects.

Creating an object

const person = {
  firstName: "Ada",
  lastName: "Lovelace",
  age: 36,
  job: "Mathematician"
};

Reading properties

Two ways to access a property:

person.firstName;  // dot notation
person["firstName"]; // bracket notation

The structure

  • Key (or property name): the label, like firstName.
  • Value: the data, like "Ada".
  • Keys and values are separated by colons.
  • Properties are separated by commas.

Why objects?

Objects group related data together. Instead of separate variables, one object holds everything about a thing:

const car = { brand: "Toyota", year: 2020, color: "red" };

Updating a property

person.age = 37;

TL;DR

  • Objects store data as key-value pairs.
  • Access with dot or bracket notation.
  • Objects group related data together.
  • Update a property by assigning to it.