Accessing Object Properties in JavaScript
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
Dot notation is the primary and simplest method for accessing properties. You refer to the object, add a dot, and specify the property name.
1234567891011const 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
- If you attempt to access a property that does not exist, JavaScript will return
undefined; undefinedis not outputted when you run the code.
Accessing Properties Through Square Brackets
Square brackets are used when the property name is not known in advance, or when it contains spaces or special characters, or when it is stored in a variable.
1234567891011const 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
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
Objects often contain other objects. Using dot notation, you can access deeply nested values by chaining property names.
1234567891011121314151617const 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
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.
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
Accessing Object Properties in JavaScript
Swipe to show menu
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
Dot notation is the primary and simplest method for accessing properties. You refer to the object, add a dot, and specify the property name.
1234567891011const 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
- If you attempt to access a property that does not exist, JavaScript will return
undefined; undefinedis not outputted when you run the code.
Accessing Properties Through Square Brackets
Square brackets are used when the property name is not known in advance, or when it contains spaces or special characters, or when it is stored in a variable.
1234567891011const 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
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
Objects often contain other objects. Using dot notation, you can access deeply nested values by chaining property names.
1234567891011121314151617const 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
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.
Thanks for your feedback!