1

What is the difference between this

var person = {
    name: "Bob",
    age: "99"
};

and this?

var person = {
    "name": "Bob",
    "age": "99"
};

Or do they mean the same thing? If they do, what if I want the key to be an object? How would I specify the object as the key if name means "name"?

Duncan
  • 984
  • 10
  • 20
  • Object can't be the key. Object can be a value. – ElmoVanKielmo Jul 04 '13 at 19:48
  • @ElmoVanKielmo that statement is both vague and misleading. The _string_ `"object"` is a perfectly valid property name. – Matt Ball Jul 04 '13 at 19:51
  • 3
    exact duplicate of [What is the difference between object keys with quotes and without quotes?](http://stackoverflow.com/questions/4348478/what-is-the-difference-between-object-keys-with-quotes-and-without-quotes) – Bergi Jul 04 '13 at 19:55
  • @MattBall, do I say _string_ `"object"` is not a valid property name? I don't even say `x = {object: "foo"};` is invalid! It is valid but the key `object` of `x` is converted implicitly to _string_ `"object"` and has nothing to do with `object` from global namespace. Misleading? Yes, using keys like `object` or `"object"` is misleading, confusing etc. And don't try to tell me that _string_ is an _object_ too. I know that. The main thing is that whatever the key is, it will be converted to _string_ if it isn't _string_ already. No way to have "object key" with custom methods and attrs. – ElmoVanKielmo Jul 05 '13 at 07:17

3 Answers3

4

There is no difference. Quotes are only necessary if you want to use a string as a property name, but that string is not a valid identifier. Further,

An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string. However, any property name that is not a valid JavaScript identifier (for example, a property name that has space or dash, or starts with a number) can only be accessed using the square bracket notation.

(source)

Object literal syntax is covered in-depth on MDN.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 1
    Another good link I found with helpful explanations. [link](http://mathiasbynens.be/notes/javascript-properties) – Daniel Simpkins Jul 04 '13 at 19:52
  • Indeed. Mathias knows his stuff. – Matt Ball Jul 04 '13 at 19:53
  • and on most browsers, quotes are necessary if the property name is a keyword – Alnitak Jul 04 '13 at 19:54
  • @Alnitak keywords are a subset of strings which are not valid identifiers, but it can't hurt to be explicit. – Matt Ball Jul 04 '13 at 19:56
  • @MattBall, keywords are not a subset of strings which are not valid identifiers. First of all, `foo bar` is a string and is not a valid identifier and is not a keyword in any programming language I know. Furthermore, term _string_ in programming is a data type or/and class, in some languages equivalent of array of chars. `Keywords` are a subset of `words` or `tokens` - they are called `reserved words` too as they have special meaning in language grammar. But you can't even say that keyword can't be identifier - `this` is a keyword and an identifier as well in javascript. – ElmoVanKielmo Jul 05 '13 at 07:33
  • @ElmoVanKielmo I don't see how `foo bar` serves as a counterexample to my statement. By definition, [every keyword/reserved word in JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words) is not a valid identifier. From that linked page: _"The following are keywords and may not be used as variables, functions, methods, or **object identifiers**"_ (emphasis added) – Matt Ball Jul 05 '13 at 15:37
  • @MattBall, I say you are using _string_ term in wrong context. – ElmoVanKielmo Jul 05 '13 at 15:41
0

They are equivalent in this case, but the quoted version allows you to use keys that are not valid JS identifiers. For example, this does not work:

{ -test: 42 }

while this does:

{ "-test": 42 }

You cannot specify an object as the key no matter what.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

They mean the same thing. Valid keys are identifiers, string literals, or numeric literals. See http://ecma-international.org/ecma-262/5.1/#sec-11.1.5

You can't yet use objects themselves as keys, but WeakMap objects as proposed for EcmaScript 6 will solve this. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap

1983
  • 5,882
  • 2
  • 27
  • 39