1
var car = { manyCars: {a: "Saab", "b": "Jeep"}, 7: "Mazda" };  

What's the difference between car.manyCars.a and car.manyCars.b in this example?

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
DarkLightA
  • 14,980
  • 18
  • 49
  • 57

2 Answers2

8

None whatsoever.

Quotes allow you to specify names which are not valid javascript identifiers, but both a and b are. For example, this wouldn't be legal:

var car = { a*b: "Saab" };

whereas this would be

var car = { "a*b": "Saab" };

As a*b is not a valid identifier.

Note that JSON (which is based on JavaScript) does not allow unquoted names.

Edit
An exception here, as you've noticed, you can use numbers without quoting them, which are not valid javascript identifiers. This is actually rather weird, don't have a good reason for this, probably the opportunity to shorthand the declaration. car.7 won't parse for the same reason, it is not a valid identifier, and you need to use car[7].

falstro
  • 34,597
  • 9
  • 72
  • 86
  • Thanks. So as a rule of thumb you should always use them? – DarkLightA Dec 26 '10 at 15:51
  • Also, when calling the property '7', why is car[7] used rather than car.7 (in the tutorial)? Or can you use either? – DarkLightA Dec 26 '10 at 15:53
  • 1
    @DarkLightA; It depends a little on context, if you intend to use your objects as traditional objects, I prefer not to use quotes (as you do want to access them using the '.'-operator). If instead, you're using it as a general purpose associative array, I'd use quotes, and access the elements with the '[]' operator. – falstro Dec 26 '10 at 15:54
  • 1
    @DarkLightA the dot and subscript (['']) are interchangable, however subscript lets you use an expression as a key - for example, `someobj[x-1]`. – JAL Dec 26 '10 at 16:04
1

As roe said, there's no difference in the end, but it allows to you to use otherwise reserved keywords or invalid identifiers:

var x = { 'a b c' : 1, 'a%#!6!#' : 1};

It probably isn't a bad idea to always use quotes. Different javascript engines consider different things to be reserved. For example, this works fine in a browser, but causes a syntax error in Rhino:

var x = { native : true };

The Mozilla Developer Network has some good information too: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Member_Operators

Dot notation

some_object.property

property must be a valid JavaScript identifier, i.e. a sequence of alphanumerical characters, also including the underscore ("_") and dollar sign ("$"), that cannot start with a number. For example, object.$1 is valid, while object.1 is not.

Bracket notation

some_object[property]

property is a string. The string does not have to be a valid identifier; it can have any value, e.g. "1foo", "!bar!", or even " " (a space).

Community
  • 1
  • 1
nickf
  • 537,072
  • 198
  • 649
  • 721
  • good point on the reserved words, although it you might not want to use those as names anyway, as `x.for` wouldn't work (at least I think it doesn't, does it?) – falstro Dec 26 '10 at 15:59
  • 1
    It does work: `x['for']` –  Dec 26 '10 at 16:02
  • Actually, that was a bad example. This works for me: `x = { for : 1}; alert(x.for);` – nickf Dec 26 '10 at 16:08
  • @M28; I said, `x.for`, not `x['for']` (which of course works, just as `var f="for"; x[f]`). @nickf, that's very interesting. I'm not so sure that's legal though, isn't `for` a reserved word? Might mess up in some parsers. – falstro Dec 26 '10 at 16:21
  • Well, my solution is granted to work on any javascript parser, for can be a keyword on some parser. –  Dec 27 '10 at 13:15
  • @M28; while correct, your 'solution' has very little to do with the problem at hand in this case, as we're talking specifically about *unquoted* property names. – falstro Jan 04 '11 at 07:49