The course is part of this learning path
- Learn how to access a value from a property that's in an object
If you have any feedback related to this course, please contact us at firstname.lastname@example.org.
Next, I'm going type another property, with the key name of "model" colon, and then in quotes, a string value of "Bronco" comma. And I'm gonna pause for a moment before completing this object, I'm gonna zoom in and point out this key value pair structure. The key name make is written directly as part of the object. Note, there are no quotes. The colon is the separator between the key and the value. And then to the right of the colon is the value. Here, the string Ford wrapped in quotes. Then there's a comma and the comma is what separates one property from another.
Now, how do we access a specific value from a property that's in an object? Well, there are two ways to access object property values, and I wanna console log each of these methods. First, I'm gonna comment out the car console log using what is called "dot notation". I gonna console log the cars all wheel drive property value. Console dot log, car dot AWD. Next using square bracket notation. I'm gonna console log the car objects, trim value, console dot log, car square brackets. When using square brackets, we have to wrap the property name in quotes. Trim.
Now I'm gonna clear the console and run the console logs. We can see the values for each of these properties. The first console log is using dot notation. The second console log is using square bracket notation. Now, is there any real difference between these two methods? Well, any property name that has a special character or number can only be accessed using square bracket notation.
Now noting that the trim property is an array. If I wanted to access a specific value from that array, I would have to use an additional square bracket notation with the index value of that element that I wish to access. So for example, I'm gonna go back to the trim console log add a square bracket and put in a value of one. To get the second element in the array. I will run this again and in the console log now, instead of the array, I have the element value of XLT.
Now I'm gonna comment the cell. And the next question that needs to be asked is, "How do we change property values in an existing object?" We can do so using the same methods we use to access the values. So I'm gonna start by typing car dot make equals Toyota, and then next, car dot model equals Corolla. And now using square bracket notation, car square bracket in quotes, AWD equals false.
Now, un comment console dot log car and hit run. Now in the console, we can see that the make property has a value of Toyota. The model property has a value of Corolla and the AWD property, has a value of false. Now, can we use the same methods to add a new property and value in an existing object? Absolutely.
So on the next line below using dot notation, I will type car dot doors equal four. And on the line below using square bracket notation, I will type car square brackets wrapped in quotes, navigation equal true. And now I'm gonna run this in the console again. And now to the right, the car object has two new properties, doors with a value of four and navigation with a value of true. And that's it. Thanks for watching at Cloud Academy.
Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.
Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.
Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.
Farish is an avid powerlifter, sushi lover, and occasional Funko collector.