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 trialAakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsHow 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-
- as a property in questions object
var questions = [
{
question: 'How many states are in the United States?',
answer: 50
}
];
- 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.
2 Answers
Steven Parker
231,236 PointsA 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.
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsOk ...it looks like I am getting something.
Now what you mean is "there is no meaning of scope to object properties"?
Steven Parker
231,236 PointsProperties don't have a separate scope. Their scope is the same as the object itself.
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsOh 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
231,236 PointsThere 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
Full Stack JavaScript Techdegree Student 11,638 PointsThanks 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
231,236 PointsAakash Srivastav — Glad to help. You can mark the question solved by choosing a "best answer".
And happy coding!
Andrew Folts
14,238 PointsYou'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).
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsAnd then what is the scope of questions variable?
Andrew Folts
14,238 PointsAlso global..
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsWhen both (question(property) and question(variable declared within for loop)) are declared within global scope then why is not there any problem at all?
Steven Parker
231,236 PointsThe only thing declared in the loop is the variable "i".
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsSorry , I have edited my question .
And I have got my answer Steven :) .
Thanks
eslam said
Courses Plus Student 6,734 Pointseslam said
Courses Plus Student 6,734 Pointsso i misunderstood your question thx steven for your expalnations