Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialroberto benites
2,897 PointsCan someone explain the "bracket" notation that s used to pass the strings? I'm not sure i fully understand how its used
in the video and code he uses the following: element[property] = value;
How are the brackets being used here? I thought brackets were used to hold arrays of objects.
1 Answer
Steven Parker
231,236 PointsBrackets are used to index arrays. But they're also one of two ways to access object properties
The more common property access method is dot notation. And example would be "myobject.propname
". But the same thing can be accessed with bracket notation: "`myobject['propname']". And in the example shown above, "property" is a string variable containing the property name to be accessed (since it's not a quoted string literal).
roberto benites
2,897 Pointsroberto benites
2,897 PointsThanks! That's what i thought but was not 100% sure.
Is there a benefit to using one versus the other?
Steven Parker
231,236 PointsSteven Parker
231,236 PointsThe advantage will become obvious in the situation. Dot notation is normally more concise and convenient, but bracket notation is the only choice when the property name is not known while programming but will be supplied in a variable.