Consider the following code:
var friends = {
bill: {
firstName: "Bill",
lastName:"William",
number: "212-555-1212",
address: ['Main Street', 'Yakama', 'WA','90012']
},
steve: {
firstName: "Steve",
lastName: "Steven",
number: "818-555-1212",
address: ['Any Street', 'Eureka', 'CA','90013']
},
betty: {
firstName: "Betty",
lastName: "Draper",
number: "503-555-1212",
address: ['Your Street', 'Madison', 'WI','90014']
}
};
var search = function(name){
for(var prop in friends){
if(friends[prop].firstName === name){
console.log(friends[prop]);
return friends[prop];
}
}
};
My question is specifically regarding this portion:
if(friends[prop].firstName === name)
I want to understand the usage of the brackets around [prop] and why they are necessary in this instance. I am very familiar with dot syntax notation but not the use of these brackets.
With regards to whether or not my question is a duplicate: the question offered as preceding mine and asking the same question is actually a question of preference i.e. what are the advantages of using dot syntax vs bracket notation. My question is far more fundamental in nature and asks why use it at all and more specifically why is it necessary in the particular instance I've shown. Furthermore I do not see that my more fundamental question is answered by that other post.
A word to those who have derided me and voted down my question, called me lazy and that did not search the web or turn my head to the right: I did search the web for an answer but there are tons of web pages with various questions related but not quite or at all precisely answering my question. Furthermore, if you do not search the web with right phrases or keywords you may never find the answer you are seeking. I am not familiar with this particular syntax in JS so perhaps I was not searching with the right phrases or keywords.
The bottom line is, I asked the question in earnest to better myself as a programmer. If my only failing here is that some among you feel it a cardinal programming sin for trying to learn or for not knowing it all, that is not my problem that certain individuals who have commented here have issues with pettiness and arrogance.
That said, I do greatly appreciate those individuals who actually tried in good faith to answer my question, much appreciated.