When using objects, programmers oftentimes want to retrieve or change the value of one of the properties. To access the value of a property, you will need the object's name and the key of the property.
Programmers have two ways to access the value of property:
To access a property with bracket syntax, the code looks like: object["key"]
.
To access a property with dot notation, the code looks like: object.key
. Notice that the key is no longer surrounded by quotes. However, keys are still strings.
Note
Recall, the only restraint in naming a key is that it has to be a valid JavaScript string. Since a key could potentially have a space in it, bracket syntax would be the only way to access the value in that property because of the quotes.
Example
1let tortoiseOne = {
2 species: "Galapagos Tortoise",
3 name: "Pete",
4 weight: 919,
5 age: 85,
6 diet: ["pumpkins", "lettuce", "cabbage"]
7};
8
9console.log(tortoiseOne["name"]);
10console.log(tortoiseOne.name);
Console Output
Pete
Pete
A programmer can modify the value of a property by using either notation style.
Warning
Recall that mutability means that a data structure can be modified without making a copy of that structure. Objects are mutable data structures. When you change the value of a property, the original object is modified and a copy is NOT made.
Example
In our zoo software, we may want to update Pete's weight as he has gained 10 lbs. We will use both bracket syntax and dot notation for our software, but that is not a requirement! Feel free to use whichever one suits your needs and is easiest for you and your colleagues to read.
1let tortoiseOne = {
2 species: "Galapagos Tortoise",
3 name: "Pete",
4 weight: 919,
5 age: 85,
6 diet: ["pumpkins", "lettuce", "cabbage"]
7};
8
9console.log(tortoiseOne.weight);
10
11newWeight = tortoiseOne.weight + 10;
12
13tortoiseOne["weight"] = newWeight;
14
15console.log(tortoiseOne["weight"]);
Console Output
919
929
After declaring and initializing an object, we can add new properties at any time by using bracket syntax:
objectName["new-key"] = propertyValue;
Example
1let tortoiseTwo = {
2 species: "Galapagos Tortoise",
3 name: "Pete",
4 weight: 919
5};
6
7console.log(tortoiseTwo);
8
9tortoiseTwo["age"] = 120;
10tortoiseTwo["speed"] = 'Faster than the hare.'
11
12console.log(tortoiseTwo);
13console.log(tortoiseTwo.age);
Console Output
{ species: 'Galapagos Tortoise', name: 'Pete', weight: 919 }
{ species: 'Galapagos Tortoise',
name: 'Pete',
weight: 919,
age: 120,
speed: 'Faster than the hare.' }
120
All of the questions below refer to an object called giraffe
.
1let giraffe = {
2 species: "Reticulated Giraffe",
3 name: "Cynthia",
4 weight: 1500,
5 age: 15,
6 diet: "leaves"
7};
Question
We want to add a method after the diet
property for easily increasing Cynthia's age on her birthday.
Which of the following is missing from our method? You can select MORE than one.
birthday: function () {age = age + 1;}
return
this
diet
Question
Could we use bracket syntax, dot notation, or both to access the properties of giraffe
?