Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Managing Object Properties | Fundamentals of JavaScript Objects
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
JavaScript Data Structures

bookManaging 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.

12345678910111213141516
const 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
copy

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.

1234567891011121314
const company = { title: "Toyota", nativeName: "トヨタ自動車株式会社", industry: "Automotive", founded: { year: 1937, month: "August", day: 28, }, }; company.founder = "Kiichiro Toyoda"; console.log(company.founder); // Output: Kiichiro Toyoda
copy

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.

12345678910
const 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
copy

Using shorthand properties, the same object can be created more succinctly:

12345678910
const name = "Carl Benz"; const birthCountry = "Germany"; const person = { name, birthCountry, }; console.log(person.name); // Output: Carl Benz console.log(person.birthCountry); // Output: Germany
copy

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?

question mark

After creating an object, how can we change the values of its properties?

Select the correct answer

question mark

What happens when you attempt to modify the value of a property that doesn't exist in an object using dot notation?

Select the correct answer

question mark

Which of the following examples demonstrates using shorthand properties for object creation?

Select the correct answer

question mark

How can you include the variable city as a property of userLocation using shorthand properties?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 7

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookManaging 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.

12345678910111213141516
const 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
copy

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.

1234567891011121314
const company = { title: "Toyota", nativeName: "トヨタ自動車株式会社", industry: "Automotive", founded: { year: 1937, month: "August", day: 28, }, }; company.founder = "Kiichiro Toyoda"; console.log(company.founder); // Output: Kiichiro Toyoda
copy

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.

12345678910
const 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
copy

Using shorthand properties, the same object can be created more succinctly:

12345678910
const name = "Carl Benz"; const birthCountry = "Germany"; const person = { name, birthCountry, }; console.log(person.name); // Output: Carl Benz console.log(person.birthCountry); // Output: Germany
copy

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?

question mark

After creating an object, how can we change the values of its properties?

Select the correct answer

question mark

What happens when you attempt to modify the value of a property that doesn't exist in an object using dot notation?

Select the correct answer

question mark

Which of the following examples demonstrates using shorthand properties for object creation?

Select the correct answer

question mark

How can you include the variable city as a property of userLocation using shorthand properties?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 7
some-alt