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

Зміст курсу

JavaScript Data Structures

Accessing Object PropertiesAccessing Object Properties

We will explore two methods for accessing object properties: dot notation and square brackets. These methods allow you to retrieve specific values from objects, and we'll discuss scenarios in which each method is commonly used.

Dot Notation to Access Properties

The primary and most frequently used method for accessing object properties is dot notation. With this approach, we access a property by specifying the object's name followed by a dot and the property name.

Let's consider an example where we use an object to represent an employee and access its properties using dot notation:

Note

  • If you attempt to access a property that does not exist, JavaScript will return undefined;
  • undefined is not outputted when you run the code.

Accessing Properties Through Square Brackets

The second method for accessing object properties is through square brackets. This syntax involves specifying the object's name, followed by square brackets with the property name as a string inside them.

This approach is used less frequently than dot notation but is essential in cases where the property name is not known in advance or is stored in a variable, such as a function parameter. Here's an example using the same employee object:

This method provides the property name as a string within square brackets. It allows for dynamic property access, which can be helpful when dealing with more complex data.

Accessing Nested Properties

Let's explore how to access nested properties using dot notation with the following example.

With dot notation, we specify the path to the desired property, separated by dots. Let's consider an example with a course object:

In this example, we access properties at different levels of nesting within the product object.

1. What are the methods for accessing object properties?
2. When using dot notation to access object properties, what is returned if you try to access a property that does not exist?
3. Consider the following complex object representing a person's contact information. How could you retrieve the postal code from the `person` object? Select all possible options.

question-icon

What are the methods for accessing object properties?

Виберіть кілька правильних відповідей

When using dot notation to access object properties, what is returned if you try to access a property that does not exist?

Виберіть правильну відповідь

question-icon

Consider the following complex object representing a person's contact information. How could you retrieve the postal code from the person object? Select all possible options.

Виберіть кілька правильних відповідей

Все було зрозуміло?

Секція 2. Розділ 5
course content

Зміст курсу

JavaScript Data Structures

Accessing Object PropertiesAccessing Object Properties

We will explore two methods for accessing object properties: dot notation and square brackets. These methods allow you to retrieve specific values from objects, and we'll discuss scenarios in which each method is commonly used.

Dot Notation to Access Properties

The primary and most frequently used method for accessing object properties is dot notation. With this approach, we access a property by specifying the object's name followed by a dot and the property name.

Let's consider an example where we use an object to represent an employee and access its properties using dot notation:

Note

  • If you attempt to access a property that does not exist, JavaScript will return undefined;
  • undefined is not outputted when you run the code.

Accessing Properties Through Square Brackets

The second method for accessing object properties is through square brackets. This syntax involves specifying the object's name, followed by square brackets with the property name as a string inside them.

This approach is used less frequently than dot notation but is essential in cases where the property name is not known in advance or is stored in a variable, such as a function parameter. Here's an example using the same employee object:

This method provides the property name as a string within square brackets. It allows for dynamic property access, which can be helpful when dealing with more complex data.

Accessing Nested Properties

Let's explore how to access nested properties using dot notation with the following example.

With dot notation, we specify the path to the desired property, separated by dots. Let's consider an example with a course object:

In this example, we access properties at different levels of nesting within the product object.

1. What are the methods for accessing object properties?
2. When using dot notation to access object properties, what is returned if you try to access a property that does not exist?
3. Consider the following complex object representing a person's contact information. How could you retrieve the postal code from the `person` object? Select all possible options.

question-icon

What are the methods for accessing object properties?

Виберіть кілька правильних відповідей

When using dot notation to access object properties, what is returned if you try to access a property that does not exist?

Виберіть правильну відповідь

question-icon

Consider the following complex object representing a person's contact information. How could you retrieve the postal code from the person object? Select all possible options.

Виберіть кілька правильних відповідей

Все було зрозуміло?

Секція 2. Розділ 5
some-alt