0

Is their any difference on accessing object properties with the dot operator then accessing them with the bracket notation? example.

var objects={
"name":"john",
"age":23,
"friends":["mike","harry"]
};
document.write(objects["friends"]); 

prints out exactly what this would

var objects={
"name":"john",
"age":23,
"friends":["mike","harry"]
};
document.write(objects.friends);

in the learning stage and curious if their is more to this? Thanks in advance.

Ali Mansour
  • 55
  • 1
  • 8

2 Answers2

0

functionality wise there is no difference, but object["property'] gives you functionality to pass property name as a parameter, while in case of object.Property you can not pass property name..

Shobhit Walia
  • 496
  • 4
  • 19
0

var person = {
"first name": "Mad",
"last name": "Max",
};

console.log(person["first name"], person["last name"]);

There is no difference between accessing properties using the dot operator or via string name.

That being said, where the string name accessing comes in handy is a case as follows:

Say if you have an object person with properties: first name, last name.

person = {
"first name": "Mad",
"last name": "Max",
}

Now you can access person["first name"] and person["last name"]

So here you can include spaces and any other otherwise invalid property names that are to be accessed via dot operator.

aliasav
  • 3,048
  • 4
  • 25
  • 30