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 trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects Mixing and Matching Arrays and Objects

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

How does using the identical names for variables and object properties not cause problems?

First I want to say that , this question has been asked in the community but the person who asked this question didn't got the answer so I decided to ask the question again.

So the question is-

Dave has used question variable two times in the program-

  1. as a property in questions object
var questions = [
    { 
        question: 'How many states are in the United States?', 
        answer: 50 
    }
];
  1. as a variable
var question;
var answer;

Also both are used at the same time-

for ( var i = 0; i < questions.length; i += 1) {
    question = questions[i].question;
    answer = questions[i].answer;
    etc. rest of code

Why doesn't it cause problem? I know there is some concept of scope but I never read about scope of object property. Please help me.

eslam said
eslam said
Courses Plus Student 6,734 Points

so i misunderstood your question thx steven for your expalnations

2 Answers

Steven Parker
Steven Parker
231,275 Points

A property can only be accessed in reference to the object, as seen in this code where the membership operator (period) is used. From the perspective of the system, there's no relationship between variables and object properties based on name.

So it might create an opportunity for confusion to a human reader, but these are entirely separate things to the system.

Steven Parker
Steven Parker
231,275 Points

Properties don't have a separate scope. Their scope is the same as the object itself.

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

Oh I am again confused .I properties is in the scope of Object itself and "Object" is in the scope global scope.
Also the variable declared later within for loop is also in global scope. Then is there not any problem?

Steven Parker
Steven Parker
231,275 Points

There is only one "question" variable, and it is in the global scope. There is nothing special about using it in a loop.

There is also no problem having the same name for a property and for a variable. They are completely different things to the system and will not be confused.

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

Thanks Steven . :) With the help of you and Jennifer(treehouse moderator) , I understood this completely. I posted the same question in the slack channel too.
Thanks again :)

Steven Parker
Steven Parker
231,275 Points

Aakash Srivastav — Glad to help. You can mark the question solved by choosing a "best answer".
And happy coding!

Andrew Folts
Andrew Folts
14,238 Points

You're right in thinking that it has to do with scope.

The QUESTION PROPERTY is inside the scope of the questions variable (more specific).

The QUESTION VARIABLE is in the global scope (more general).