Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Working with Properties | Objects Fundamentals
JavaScript Data Structures
course content

Course Content

JavaScript Data Structures

JavaScript Data Structures

1. Introduction and Prerequisites
2. Objects Fundamentals
3. Advanced Object Manipulation
4. Mastering Arrays
5. Advanced Array Operations

bookWorking with 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 creating an object, you can easily change the values of its properties. To do this, we simply refer to the property by name, using dot notation, and assign a new value.

Let's consider an example with a company object:

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 new property to an object is no different from changing the value of an existing property. If a property with a specified name doesn't already exist in the object, it will be created.

Consider the company object from the previous example:

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 simplify the process of creating objects, especially when you want to use variable or function parameter values as property values.

This syntax allows us to use the variable name as the property name and its value as the property value.

Consider the following example:

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?
After creating an object, how can we change the values of its properties?

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

Select the correct answer

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

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

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

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

Select the correct answer

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

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