Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Array Iteration Methods Combining Array Methods Working with Objects in Arrays

2 Answers

Steven Parker
Steven Parker
210,425 Points

The entire expression is "usersObject[user.name]", so bracket notation is being used to access a specific property of the "usersObject". The value in user.name is the name of the property being created.

Daniel Benisti
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Benisti
Front End Web Development Techdegree Graduate 22,223 Points

but if i want to acces property in object with bracket notation i need to add a string with it , am i right? like this const peoples ={ name: dani, age: 22 } peoples["name"]

Steven Parker
Steven Parker
210,425 Points

That's right, but the string can be a literal (as in your example) or it can come from a variable (as what user.name holds).

Jesus Mendoza
Jesus Mendoza
23,273 Points

When using variables to access elements inside an object you have to use [].

For example:

// Using plain property names
const obj = {
  name: 'Jesús'
}
console.log(obj.name) // logs Jesús

// Using variables
const propertyName = 'name';
const obj2 = {
  name: 'Jesús'
};
console.log(obj[propertyName]); // logs Jesús