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 trialSean Gibson
38,363 Pointsdot vs. no dot in constant declaration
I'm a bit confused about this part of the code:
const text = cards[id][side];
const hint = cards[id].hint;
Why does hint use dot notation ( [id].hint) while text does not? Are question/answer and hint not all properties of a specific json object (cards[1].property)? When I try changing to text = cards[id].[side] it crashes. Is it perhaps because side is not specifically defined as a property we should treat this part as a 2-dimension array? I'm merely speculating at this point so could someone please help out?
2 Answers
Jason Larson
8,361 PointsI have not watched the video, but your question is about object properties. There are 2 ways to specify the properties of an object. The one that is more common is the dot notation, as in
const hint = cards[id].hint;
You can also refer to an object property by using bracket notation, where you specify the property name inside the brackets, like
const text = cards[id][side];
You had 2 problems when you changed it to text = cards[id].[side]
. First, you combined dot notation with the bracket notation. If you're going to do it like that, it should be const text = cards[id].side;
. Unfortunately, you can't use dot notation like that if the property name is stored in a variable. I believe dot notation may just be syntactic sugar for the bracket notation and is expecting the value after the dot to be a property name, and does not convert it from a string variable.
As for your question about treating it like a 2d array, that would only be applicable if cards[id] is an array and side is a number. Since you're other example apparently works, I'm guessing cards[id] is an object and not an array. I'm also assuming side
in this code represents a string.
Sean Gibson
38,363 PointsThanks, Jason. Your answer was quite helpful in reminding me of bracket vs. dot notation. In the video, side is a variable that will (depending on the url query string) be either question or answer, which do happen to be properties of the JSON object cards[x]. However, if I change cards[id][side] to cards[id].side the program doesn't run properly. It doesn't crash, but it doesn't display properly. Is this perhaps because when using variables one MUST use bracket notation? That's the only thing I can come up with. Please tell me I'm at least on the right track ;-)
Jason Larson
8,361 PointsSean, you're absolutely right. I forgot about the requirement for using bracket notation when using a variable for the property name. I knew there were times when you had to use bracket notation, I just forgot what they were. I'll update my original answer so that anybody else with the same question won't have the same issue you did.