12.3. Coding With Objects

12.3.1. Booleans and Objects

Objects are not stored by their properties or by value, but by reference. Storing something by reference means that it is stored based on its location in memory. This can lead to some confusion when comparing objects.

Example

Let's see how this affects our zoo software! Surely, the zoo has more than one tortoise. The second tortoise is named Patricia!

 1let tortoiseTwo = {
 2   species: "Galapagos Tortoise",
 3   name: "Patricia",
 4   weight: 800,
 5   age: 85,
 6   diet: ["pumpkins", "lettuce", "cabbage"],
 7   sign: function() {
 8      return this.name + " is a " + this.species;
 9   }
10 };

Because Pete and Patricia are members of the same species, are the same age, and have the same diet, you might notice that many of their properties are equal, but some are not. Pete weighs more than Patricia and of course, they have different names!

For this example, we will only keep the species and diet properties.

 1let tortoiseOne = {
 2   species: "Galapagos Tortoise",
 3   diet: ["pumpkins", "lettuce", "cabbage"]
 4};
 5
 6let tortoiseTwo = {
 7   species: "Galapagos Tortoise",
 8   diet: ["pumpkins", "lettuce", "cabbage"]
 9};
10
11console.log(tortoiseOne === tortoiseTwo);

Console Output

false

The objects contain properties that have the same keys and equal values. However, the output is false.

Even though tortoiseOne and tortoiseTwo have the same keys and values, they are stored in separate locations in memory. This means that even though you can compare the properties in different objects for equality, you cannot compare the objects themselves for equality.

12.3.2. Iterating Through Objects

We can iterate through all of the values in an object, much like we would do with an array. We will use a for loop to do that, but with a slightly different structure. for...in loops are specifically designed to loop through the properties of an object. Each iteration of the loop accesses a key in the object. The loop stops once it has accessed every property.

Example

 1let giraffe = {
 2  species: "Reticulated Giraffe",
 3  name: "Cynthia",
 4  weight: 1500,
 5  age: 15,
 6  diet: "leaves"
 7};
 8
 9for (item in giraffe) {
10   console.log(item + ", " + giraffe[item]);
11}

Console Output

species, Reticulated Giraffe
name, Cynthia
weight, 1500
age, 15
diet, leaves

In this example, item is a variable that holds the string for each key. It is updated with each iteration of the loop.

Note

Inside a for..in loop, we can only use bracket syntax to access the property values.

Try It!

Write a for..in loop to print to the console the values in the tortoiseOne object. Try it at repl.it

12.3.3. Objects and Functions

Programmers can pass an object as the input to a function, or use an object as the return value of the function. Any change to the object within the function will change the object itself.

Example

 1let giraffe = {
 2  species: "Reticulated Giraffe",
 3  name: "Cynthia",
 4  weight: 1500,
 5  age: 15,
 6  diet: "leaves"
 7};
 8
 9function birthday(animal) {
10    animal.age = animal.age + 1;
11    return animal;
12}
13
14console.log(giraffe.age);
15
16birthday(giraffe);
17
18console.log(giraffe.age);

Console Output

15
16

On line 16, when the birthday function is called, giraffe is passed in as an argument and returned. After the function call, giraffe.age increases by 1.

12.3.4. Check Your Understanding

Question

What type of loop is designed for iterating through the properties in an object?

Question

Given the following object definitions, which statement returns true?

 1let tortoiseOne = {
 2   age: 150,
 3   species: "Galapagos Tortoise",
 4   diet: ["pumpkins", "lettuce", "cabbage"]
 5};
 6
 7let tortoiseTwo = {
 8   age: 150,
 9   species: "Galapagos Tortoise",
10   diet: ["pumpkins", "lettuce", "cabbage"]
11};
  1. tortoiseOne == tortoiseTwo
  2. tortoiseOne === tortoiseTwo
  3. tortoiseOne.age === tortoiseTwo.age