Who's in your Bracket?


#1

I understand the syntax and completed the instructions without error. My question is why does the variable aProperty work? In the line var aProperty = "job" , why does it not see "job" as a string? 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

I was having the same problem and I know the code is correct, so I went online and saw that if you refresh the page it will take it. I am not sure why this happens, but you should try it.


#3

If i understood it correctly, when you tell it to log a variable with a bracket, it will look for that bracket it's refering too(Brackets are like tags in JavaScript, if i understood corretly). Please correct me asap if i am wrong, hehe.


#4

Thank you. I confirmed this by removing the variable james and the brackets from the console.log and it returned the string job. So, that variable is just a string unless it's called properly. If you call:

console.log(aProperty) // Valid for returning "job"
console.log(james[aProperty]) // valid for returning a property of james called job.

Thank you again. I understand it now.


#5

Thank you. In this case I almost wish it wasn't taking my code. LOL. When I submitted my code it worked perfect but I had no idea what I had just done.

I should have plowed on because a couple lessons later they actually explain how using brackets to call properties within objects works much better.

Thank you for your response!


#6

How that happened !! when I have refreshed the page, the code have run although I wrote it correctly but it hasn't run. !?! :innocent: