Managing Object Properties
Let's examine three important concepts related to working with object properties:
- Modifying property values;
- Adding new properties;
- Using shorthand properties.
Modifying Property Values
After an object is created, you can easily update any of its properties. To modify a value, use dot notation and assign a new value to the desired property.
12345678910111213141516const company = { title: "Toyota", nativeName: "トヨタ自動車株式会社", industry: "Pharmacy", founded: { year: 1996, month: "August", day: 28, }, }; company.industry = "Automotive"; company.founded.year = 1937; console.log(company.industry); // Output: Automotive console.log(company.founded.year); // Output: 1937
In this example, we change the values of the industry and founded.year properties.
Adding New Properties
Adding a property is no different from modifying one. If the property name does not exist, JavaScript automatically creates it.
1234567891011121314const company = { title: "Toyota", nativeName: "トヨタ自動車株式会社", industry: "Automotive", founded: { year: 1937, month: "August", day: 28, }, }; company.founder = "Kiichiro Toyoda"; console.log(company.founder); // Output: Kiichiro Toyoda
In this example, we add the new property founder to the company object.
Using Shorthand Properties
Shorthand properties allow you to create objects more concisely. When the property name and variable name are the same, JavaScript lets you skip the repetition.
12345678910const name = "Carl Benz"; const birthCountry = "Germany"; const person = { name: name, birthCountry: birthCountry, }; console.log(person.name); // Output: Carl Benz console.log(person.birthCountry); // Output: Germany
Using shorthand properties, the same object can be created more succinctly:
12345678910const name = "Carl Benz"; const birthCountry = "Germany"; const person = { name, birthCountry, }; console.log(person.name); // Output: Carl Benz console.log(person.birthCountry); // Output: Germany
With shorthand properties, we only need to specify the property name, and the value is automatically taken from a variable with the same name.
1. After creating an object, how can we change the values of its properties?
2. What happens when you attempt to modify the value of a property that doesn't exist in an object using dot notation?
3. Which of the following examples demonstrates using shorthand properties for object creation?
4. How can you include the variable city as a property of userLocation using shorthand properties?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 2.27
Managing Object Properties
Swipe to show menu
Let's examine three important concepts related to working with object properties:
- Modifying property values;
- Adding new properties;
- Using shorthand properties.
Modifying Property Values
After an object is created, you can easily update any of its properties. To modify a value, use dot notation and assign a new value to the desired property.
12345678910111213141516const company = { title: "Toyota", nativeName: "トヨタ自動車株式会社", industry: "Pharmacy", founded: { year: 1996, month: "August", day: 28, }, }; company.industry = "Automotive"; company.founded.year = 1937; console.log(company.industry); // Output: Automotive console.log(company.founded.year); // Output: 1937
In this example, we change the values of the industry and founded.year properties.
Adding New Properties
Adding a property is no different from modifying one. If the property name does not exist, JavaScript automatically creates it.
1234567891011121314const company = { title: "Toyota", nativeName: "トヨタ自動車株式会社", industry: "Automotive", founded: { year: 1937, month: "August", day: 28, }, }; company.founder = "Kiichiro Toyoda"; console.log(company.founder); // Output: Kiichiro Toyoda
In this example, we add the new property founder to the company object.
Using Shorthand Properties
Shorthand properties allow you to create objects more concisely. When the property name and variable name are the same, JavaScript lets you skip the repetition.
12345678910const name = "Carl Benz"; const birthCountry = "Germany"; const person = { name: name, birthCountry: birthCountry, }; console.log(person.name); // Output: Carl Benz console.log(person.birthCountry); // Output: Germany
Using shorthand properties, the same object can be created more succinctly:
12345678910const name = "Carl Benz"; const birthCountry = "Germany"; const person = { name, birthCountry, }; console.log(person.name); // Output: Carl Benz console.log(person.birthCountry); // Output: Germany
With shorthand properties, we only need to specify the property name, and the value is automatically taken from a variable with the same name.
1. After creating an object, how can we change the values of its properties?
2. What happens when you attempt to modify the value of a property that doesn't exist in an object using dot notation?
3. Which of the following examples demonstrates using shorthand properties for object creation?
4. How can you include the variable city as a property of userLocation using shorthand properties?
Thanks for your feedback!