Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Accessing Object Properties | Objects Fundamentals
JavaScript Data Structures
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

bookAccessing 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:

1234567891011
const employee = { companyName: "Schuster, Mertz and Marks", name: "Miss Alma Boyer", address: "2277 Karine Plains", workedYears: 4, remote: false, }; console.log(employee.name); // Output: Miss Alma Boyer console.log(employee.address); // Output: 2277 Karine Plains console.log(employee.lastName); // Output: undefined
copy

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:

1234567891011
const employee = { companyName: "Schuster, Mertz and Marks", name: "Miss Alma Boyer", address: "2277 Karine Plains", workedYears: 4, remote: false, }; console.log(employee["name"]); // Output: Miss Alma Boyer console.log(employee["address"]); // Output: 2277 Karine Plains console.log(employee["lastName"]); // Output: undefined
copy

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:

1234567891011121314151617
const course = { courseName: "Applied Science", courseDuration: "48 hours", author: { position: "Nuclear Physicist", age: 43, name: { first: "Mattie", last: "Crooks", }, }, }; console.log(course.author.position); // Output: Nuclear Physicist console.log(course.author.age); // Output: 43 console.log(course.author.name.first); // Output: Mattie console.log(course.author.name.last); // Output: Crooks
copy

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.
What are the methods for accessing object properties?

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?

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

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

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.

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
We're sorry to hear that something went wrong. What happened?
some-alt