Represent Everyday Things with Objects

Codecademy Team
Expand the possibilities of what your program can do with objects.

What is an Object?

When we take a look at an item, we can come up with a few key characteristics that describe it. Take for instance, a dog. When we think of a dog, we might describe it using characteristics like its name, its body size, fur color, etc… While each individual characteristic doesn’t represent a dog, collectively we get a sense of what a dog is.

Now, let’s think about how we could represent a dog in code. We could write all the dog’s information in a string, but it would be hard to extract information afterward. We could even store all the information in an array, but then we would have to rely on the ordering to keep track of each characteristic. What if we wrote out the information like so:

let theBestDogEver = {
name: 'Air Bud',
bodySize: 'large',
furColor: 'golden'
};

Then our “characteristics” are stored as a collection of key-value pairs, like the key of name is tied with the value 'Air Bud'. What we have in the example above is a JavaScript object! Generally, these “characteristics” that are tracked with key-value pairs are known as properties. These properties don’t follow any set order, rather these properties all exist inside the dog object and we can easily retrieve and manipulate the values by the key.

As you start learning more about objects, you’ll see how you’ve actually interacted with objects before and expand the possibilities of your programs!