Confused about syntax in exercise 5 'Who's In The Bracket'


I've been able to get through this exercise having red a few examples on these boards, and despite seemingly not being the hardest exercise on the course I am a bit (a lot) confused by some of the logic here...

Namely using 'console.log(james[aProperty]);' to print the job value 'programmer'.

What is it that ties james and aProperty (other than 'aProperty' holding the same value as 'job') that allows it to be called in this way? In the code, aProperty is not directly related to 'james' in a way that would seen to make this logical - to my limited comprehension at least! console.log(aProperty) would make more sense in my mind, even though this obviously doesn't work.

Also why no inverted commas for aProperty in this console log command (between the square brackets) when you would use them for console.log(james["job"]); (which prints the same result, 'programmer').

And finally... why the inverted commas for 'job' on this line:

var aProperty = "job";

Is it just because it holds a string value, or would it be written like this even if it was a boolean value?

Many thanks for any input on this, and apologies if its all very simple/obvious!

var james = {
    job: "programmer",
    married: false

// set to the first property name of "james"
var aProperty = "job";

// print the value of the first property of "james" 
// using the variable "aProperty"


aProperty has the value, "job", a key name string, not the value, james.job. a value string. That's why,


prints, programmer. Key names are strings, and when we access them using bracket notation, we write them as strings.

console.log(james['job']);    // programmer

Since the value of aProperty is a string that matches a key name, it is treated as that key name.

This is more obvious when we write a for-in statement which has a dynamic variable...

for (var key in object) {
    console.log( key + ": "+ object[key]);

In the above, key is the dynamic variable that takes each key name, in turn as it iterates object.


Ok, many thanks for this explanation.

I think I need to do a little research on the differences between key name and value strings and dynamic variables as these seem to be the blindspots for me here.

What then would happen to aProperty if there was another object, say:

var will = {
job: "footballer",
married: true


will[aProperty] will be footballer.

key-value pair

 { "job"   : "footballer" }
//key name : value object

Keys are always strings. We leave the quotes off any that do not contain white space of invalid characters. We can write key names in our literal, if we wish. It's perfectly valid.

Values may be any object, but in thie case the value is a string object. That's why I called it the value string. Don't let this confuse you.

A dynamic variable is one that takes multiple values in a dynamic construct, such as a loop. The variable is given its values by JavaScript, not our code. The loop iterates over all the properties of the object, and the parser loads the variable with key it is pointing to. Again, don't labor over this. Concentrate on the syntax of bracket notation.

object['property']  => a definite key

object[aProperty] => a variable representation of a key

for (var key in object) {}
           => a dynamic key variable