0

I have a simple example where I can gather the value of an Object property in the following two ways:

Object

var obj = {name:"Peter", age: "23"};

Method 1

obj.name

Method 2

obj['name']

I think in both the methods we using the key of an object to access its value. So what is the difference?

OR

Why do we need 2 methods if we can do the same thing with both?

Deadpool
  • 7,811
  • 9
  • 44
  • 88

2 Answers2

1

This one obj.name is called dot notation and this one obj['name'] is called as bracket notation.

Why do we need 2 methods if we can do the same thing with both?

Bracket notation will help you to access a property which is having a key that doesn't qualify a valid variable name.

Ex:

var x = {"#12":"Hello"};
console.log(x["#12"]); //Hello

Where you can't use dot notation, like x.#12. Since that would be an invalid syntax.

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • 1
    Any other differences than the name ? means functional differences? – Deadpool Mar 09 '16 at 12:31
  • @downvoter care to comment? – Rajaprabhu Aravindasamy Mar 09 '16 at 12:35
  • Really? You think that a downvote on [an answer like this](http://stackoverflow.com/revisions/35891674/1) needs a comment? – JJJ Mar 09 '16 at 12:37
  • @Juhana Yeah I accept it. But how can I type everything in a minute. I was editing it. – Rajaprabhu Aravindasamy Mar 09 '16 at 12:38
  • Nobody says that you have to answer within a minute. Post the answer only after you've finished writing it, or accept that you can get downvotes when you post garbage just to get the first answer in. – JJJ Mar 09 '16 at 12:39
  • @Juhana Is SO have any constraint that I have to wait for certain time to post an answer? Whether it is going to be a first answer or last answer, at the end which one is gonna have a good content. That is the point. A person who used to write garbage can see garbage in everything. Let the answerers to make edits. Otherwise do whatever you want. No one is gonna care. – Rajaprabhu Aravindasamy Mar 09 '16 at 12:50
  • You posted a bad answer and got a downvote. Then you edited the answer and the downvote was removed. What's the problem? Just don't post "@downvoter care to comment" when you *know* why you got the downvote. – JJJ Mar 09 '16 at 12:56
0

Square bracket notation allows use of characters that can't be used with dot notation:

var foo = myForm.foo[]; // incorrect syntax
var foo = myForm["foo[]"]; // correct syntax

The second advantage of square bracket notation is when dealing with variable property names.

for (var i = 0; i < 10; i++) {
  someFunction(myForm["myControlNumber" + i]);
}

Roundup:

Dot notation is faster to write and clearer to read. Square bracket notation allows access to properties containing special characters and selection of properties using variables Another example of characters that can't be used with dot notation is property names that themselves contain a dot.

For example a json response could contain a property called bar.Baz.

var foo = myResponse.bar.Baz; // incorrect syntax
var foo = myResponse["bar.Baz"]; // correct syntax

Reference : From Here

Community
  • 1
  • 1
Amulya Kashyap
  • 2,333
  • 17
  • 25