Who's in your bracket? Explanation if you please


#1

Hello all. I have passed this exercise, but am still confused as to how the string value in aProperty is able to access the variable property of james. In other words, how does aProperty become a property of james? Thanks in advance.

.

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"
console.log(james[aProperty]);


#2

First, it is important to recognize that property names are strings. When we assign the string job to aProperty we are assigning a string that is also a property name.

james[aProperty]

is actually accessing

james['job']

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.