I am playing around with JS at the moment, coming from Java. I did some Tutorials and need some help with the dot and bracket notation in JS.
I made something, I consider an "Object" in JS. Its called "friends". Within this object, there is another object "bill".
var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way','Redmond','WA','98052']
};
var search = function(name) {
for(var prop in friends) {
if(friends[prop].firstName === name) {
return friends[prop];
}
}
};
search("Bill");
This is clearly working as intended. I used bracket notation in the search-function. BUT it isnt clear to me why this version wouldnt work the same way:
var search = function(name) {
for(var prop in friends) {
if(friends.prop.firstName === name) {
return friends.prop;
}
}
In my understanding, bracket and dot notation are used for the same things. I did a search on stackoverflow and I think this question goes the same way, but I think I do not fully understand whats the problem with all the quotation marks.