21

What is the real difference in using [] and . for accessing array or object properties? Which one to use?

Also why doesn't . operator allow the index property?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Maizere Pathak.Nepal
  • 2,383
  • 3
  • 27
  • 41

4 Answers4

48

Accessing members with . is called dot notation. Accessing them with [] is called bracket notation.

The dot notation only works with property names which are valid identifier names [spec], so basically any name that would also be a valid variable name (a valid identifier, see also What characters are valid for JavaScript variable names?) and any reserved keyword [spec].

Bracket notation expects an expression which evaluates to a string (or can be coerced to a string), so you can use any character sequence as property name. There are no limits to what a string can contain.

Examples:

obj.foo;  // valid
obj.else  // valid, reserved keywords are valid identifier names
obj.42    // invalid, identifier names cannot start with numbers
obj.3foo  // invalid,                ""
obj.foo-bar // invalid, `-` is not allowed in identifier names

obj[42]   // valid, 42 will be coerced to "42"
obj["--"] // valid, any character sequence is allowed
obj[bar]  // valid, will evaluate the variable `bar` and 
          // use its value as property name

Use bracket notation:

  • When the property name is contained in a variable, e.g. obj[foo].
  • The property name contains characters not permitted in identifiers, e.g. starts with a digit, or contains a space or dash (-), e.g. obj["my property"].

Use dot notation: In all other situations.

There is a caveat though regarding reserved keywords. While the specification permits to use them as property names and with the dot notation, not all browsers or tools respect this (notably older IE versions). So the best solution in my opinion is to avoid using reserved keywords for property names or use bracket notation if you cannot.


†: That's also the reason why you can only use bracket notation to access array elements. Identifiers cannot start with digits, and hence cannot consist only of digits.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
6

You should use . when you know the name of the property

var object = {};
object.property = 'whatever';

, use [] when the name of the property is contained in a variable

var object = {};
var property = 'another-property';
object[property] = 'whatever';

As @DCoder added certain object properties cannot be accessed without using the [] notation because their names break the syntax. E.g. properties named class, default, or data-prop-value

Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
  • 3
    A few examples would make this answer really great :) – Lix Jun 19 '13 at 11:11
  • Also, certain object properties cannot be accessed without using the `[]` notation because their names break the syntax. E.g. properties named `class`, `default`, or `data-prop-value`. – DCoder Jun 19 '13 at 11:17
  • 1
    @DCoder: Reserved keywords can be accessed with dot notation. – Felix Kling Jun 19 '13 at 11:22
  • @FelixKling: Unfortunately, I have encountered the reverse in IE 8. – DCoder Jun 19 '13 at 11:27
  • 2
    @DCoder: Ok, lets say according to the spec it's possible... I don't expect too much from IE anyway ;) Personally I avoid reserved keywords for property names all together. – Felix Kling Jun 19 '13 at 11:27
5

Also why doesn't . operator allow the index property? I really want full reason. Thank you.

Well if that was possible, consider:

var a = 0.5;

Did you mean the number 0.5 or access the 5 element of the number? See:

Number.prototype[5] = 3;

0[5] //3
0.5 // 0.5

If you allowed the syntax 0.5 to be equal to 0[5], then how do you know what you mean?

It is however possible to use numbers directly with object literal:

var a = {
    0: 3,
    1: 5
};
Esailija
  • 138,174
  • 23
  • 272
  • 326
0

Both dot operator and index(bracket notation) operator are used to access the property of an Object. Generally accessing with dot operator is quite faster because accessing variables by window is significantly slower though. But in case of special character in the variables, you cannot use dot operator as it will give error. For such cases we need to use index operator and pass the variable name as a string format means underdouble quote otherwise it will give undefined error. e.g-

var abc = {
    font-size : "12px"
} 
Using dot operator - abc.font-size;           //it will give error (Incorrect)
Using index operator - abc["font-size"];      //12px (Correct)
Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
Vivek Mehta
  • 734
  • 8
  • 10