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 trialTracy Reith
1,894 PointsHow does using the identical names for variables and object properties not cause problems?
I noticed in the example that the objects within the array have keys 'question' and 'answer':
var questions = [
{
question: 'How many states are in the United States?',
answer: 50
}
];
and also variables are declared using those same names
var question;
var answer;
Eventually these are used together in the for loop
for ( var i = 0; i < questions.length; i += 1) {
question = questions[i].question;
answer = questions[i].answer;
etc. rest of code
Everything works, but why is it not a problem for variables and object keys to have identical names? Is it good practice, bad practice, or personal preference?
1 Answer
Ari Misha
19,323 PointsHiya Tracy! You've asked an interesting question which you'll cover in later courses. Its called "variable scopes". I wont spoil it for you but i'd suggest you to stick with it and later you'll get to the topic of "variable scope". As far as best practice goes, at this point of learning , i'd recommend you to not worry about best practices , just focus on the learning part. You can always clean up your code later, right? If you still wanna know about "variable scopes" , just post back and lemme know. (: