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
Dayne Wright
11,235 PointsInstead of console.log(me[key]) could you use console.log(key) ?
I am going through the PHP development path and in javascript.
in this example:
var me = {
first_name: "Jim",
last_name: "Hoskins",
"Employee Number": 1
}
me["first_name"] = "James"
console.log(me.first_name);
console.log(me["last_name"]
console.log(me["Employee Number"]);
console["log"](me);
var key = "last_name";
console.log(me[key])
Could you not change the last console.log to:
console.log(key)
and then get the same result?
3 Answers
Erik McClintock
45,783 PointsDayne,
No, you would not get the same result if you simply passed the variable "key" into your console.log statement.
You're assigning the string value of "last_name" to the variable "key", which, by itself, is just that: a string value of "last_name". If you wrote "console.log(key)", it would print the string value held in that variable to the console, which would give you "last_name".
If you want to access the "last_name" property of the "me" object, you have to tell the console.log statement that you are accessing that key within that object specifically. Thus, you must write "console.log(me[key])" for that to print out the value paired with that key in that object.
Try this out for yourself and you'll see the results, which hopefully will help to clarify what is going on here. When you assign the string value of "last_name" to your variable "key", you are simply assigning a string value to a variable, like you would anywhere else. If that string value (of "last_name") happens to match the key name in a given object (i.e. the key "last_name" within your "me" object), and you pass the variable "key" (which holds the string "last_name") in as the key that you want to access in your object, JavaScript will translate that variable into its value (in this case, the string "last_name"), see that it matches a key in that object, and then return the value paired with that key, as if you had written "console.log(me["last_name"])" in the first place.
Erik
Michael Mayer
6,161 PointsNo, the result is different. When you console.log(me[key]) you're looking in the me object under a specific key. When you do this, you return the value associated with the key, not the key itself (in this case "hoskins".) Logging just "key" will just return "last_name."
Dayne Wright
11,235 PointsI see! That does make sense.
Thanks for all the help.