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
babasariffodeen
6,475 PointsQuestion about video: "Using `for in` to Loop Through an Object's Properties"
Towards the end of the video, there seems to be some contradiction made. First, Dave says that the javascript interpreter needs a string to access the property so he uses person["country"] to show us this. But then he goes on to replace that with person[prop].
So doesn't go against what he just said about it needing to be a string within the square brackets?
LINK to video: https://teamtreehouse.com/library/using-for-in-to-loop-through-an-objects-properties
1 Answer
Steven Parker
243,656 PointsA string is being used in both cases. While "country" is a string literal, prop is the name of a variable that contains a string.
Using the variable allows the loop to substitute a string with the name of each key to show the value that goes with that key.
babasariffodeen
6,475 PointsSo does this mean that the properties of an object are considered strings? i.e in the example of the video, name, skills, country age etc are themselves strings?
Because for example age has a number as its value i.e 35 which is not a string.
Just wanted to be sure. Thanks
Steven Parker
243,656 PointsYou are exactly right, the property values can be many things, including strings, numbers, functions, or even other objects.
But the property keys are all strings.
Ben Reynolds
35,170 PointsBen Reynolds
35,170 PointsCan you share a link to the video?